我的循环下面有什么问题?我想计算total_count并在地图中进行一些操作后返回[total,total],但是我得到了未定义。
我的raw
数据
const raw = [{
"device_info": {
"name": "cam1",
},
"age_range": {
"0-10": {
"age_range": "0-10",
"total_count": 15,
"male_count": 6,
"female_count": 9
},
"11-20": {
"age_range": "11-20",
"total_count": 11,
"male_count": 7,
"female_count": 4
},
"21-30": {
"age_range": "21-30",
"total_count": 922,
"male_count": 452,
"female_count": 470
}
}
}, {
"device_info": {
"name": "cam2",
},
"age_range": {
"0-10": {
"age_range": "0-10",
"total_count": 1,
"male_count": 1,
"female_count": 0
},
"11-20": {
"age_range": "11-20",
"total_count": 2,
"male_count": 0,
"female_count": 2
},
"21-30": {
"age_range": "21-30",
"total_count": 90,
"male_count": 58,
"female_count": 32
}
}
}]
循环
const x = raw.map(obj => {
return Object.keys(obj).forEach(key => {
let total = 0
if (key === 'age_range') {
total = Object.keys(obj.age_range).reduce((acum, innerKey) => {
return acum + obj.age_range[innerKey].total_count
}, 0)
console.log(total)
}
});
})
console.log('x', x)
答案 0 :(得分:2)
Array#forEach
不会返回任何内容,请改为使用Array#map
和Object.values
:
const x = raw.map(obj => {
return Object.values(obj.age_range).reduce((acc, item) => acc + item.total_count, 0)
})
工作示例:
const raw = [{
"device_info": {
"name": "cam1",
},
"age_range": {
"0-10": {
"age_range": "0-10",
"total_count": 15,
"male_count": 6,
"female_count": 9
},
"11-20": {
"age_range": "11-20",
"total_count": 11,
"male_count": 7,
"female_count": 4
},
"21-30": {
"age_range": "21-30",
"total_count": 922,
"male_count": 452,
"female_count": 470
}
}
}, {
"device_info": {
"name": "cam2",
},
"age_range": {
"0-10": {
"age_range": "0-10",
"total_count": 1,
"male_count": 1,
"female_count": 0
},
"11-20": {
"age_range": "11-20",
"total_count": 2,
"male_count": 0,
"female_count": 2
},
"21-30": {
"age_range": "21-30",
"total_count": 90,
"male_count": 58,
"female_count": 32
}
}
}]
const x = raw.map(obj => Object.values(obj.age_range).reduce((acc, item) => acc + item.total_count, 0))
console.log(x)
答案 1 :(得分:0)
你的问题是,raw.map返回来自Object.key(obj)的任何内容.forEach(...)
但是每个只是一个“迭代器”功能。它实际上没有返回任何东西。
请参阅:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
答案 2 :(得分:0)
像这样更改。在第一个map
变量中定义总数并从forEach
中删除返回.ForEach不返回任何内容。并且只在第一个地图中添加返回声明。
const raw = [{
"device_info": {
"name": "cam1",
},
"age_range": {
"0-10": {
"age_range": "0-10",
"total_count": 15,
"male_count": 6,
"female_count": 9
},
"11-20": {
"age_range": "11-20",
"total_count": 11,
"male_count": 7,
"female_count": 4
},
"21-30": {
"age_range": "21-30",
"total_count": 922,
"male_count": 452,
"female_count": 470
}
}
}, {
"device_info": {
"name": "cam2",
},
"age_range": {
"0-10": {
"age_range": "0-10",
"total_count": 1,
"male_count": 1,
"female_count": 0
},
"11-20": {
"age_range": "11-20",
"total_count": 2,
"male_count": 0,
"female_count": 2
},
"21-30": {
"age_range": "21-30",
"total_count": 90,
"male_count": 58,
"female_count": 32
}
}
}]
const x = raw.map(obj => {
let total = 0
Object.keys(obj).forEach(key => {
if (key === 'age_range') {
total = Object.keys(obj.age_range).reduce((acum, innerKey) => {
return acum + obj.age_range[innerKey].total_count
}, 0)
}
});
return total
})
console.log('x', x)