let arr = [
{year:2018,month:8,day:2,n_birth:6},
{year:2018,month:8,day:3,n_birth:6},
{year:2018,month:8,day:5,n_birth:6},
{year:2018,month:7,day:2,n_birth:6},
{year:2017,month:7,day:2,n_birth:6},
{year:2016,month:7,day:2,n_birth:6},
]
let yearNum = {}
// 统计每年出生的人数
for(let i=0;i<arr.length;i++){
let temp = arr[i];
if(yearNum.hasOwnProperty(temp.year)){
yearNum[temp.year] += temp.n_birth
}else{
yearNum[temp.year] = temp.n_birth
}
}
// 统计某一年中的具体的各月份
// { '2016': { '7': 6 },
// '2017': { '7': 6 },
// '2018': { '7': 6, '8': 18 } }
var nums ={}
for(let i=0;i<arr.length;i++){
if(nums[arr[i].year]){
if(nums[arr[i].year].hasOwnProperty([arr[i].month])){
nums[arr[i].year][arr[i].month] +=arr[i].n_birth
}else{
nums[arr[i].year][arr[i].month]=arr[i].n_birth;
}
}else{
let temp = arr[i].month.toString()
nums[arr[i].year] ={[temp]:arr[i].n_birth}
}
}
console.log(yearNum)
console.log(nums)
Object.keys(nums).forEach((item)=>{
let temp = nums[item]
Object.keys(temp).forEach((cur)=>{
console.log(item+'year'+cur+'month'+'partion'+temp[cur]/yearNum[item])
})
})