为什么x的结果是
[object Object] 12
我不能在reduce函数中返回对象吗?我知道在map中,过滤器我可以返回对象,不知道下面的代码有什么问题。
const raw = [{
"device_info": {
"name": "nokia",
"device_id": "123"
},
"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
}
}
}, {
"device_info": {
"name": "iphone",
"device_id": "456"
},
"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
}
}
}]
const x = raw.map(obj => {
return Object.values(obj.age_range).reduce((acc, obj2) => ({
total_count: acc + obj2.total_count,
device_id: obj.device_info.device_id
}), 0)
})
console.log('x', x)
不知道什么是错的,需要帮助。
答案 0 :(得分:1)
正如我已经explained给你的那样:在第一次迭代中,acc
包含0,但是你返回一个对象。在下一次迭代中,acc
将包含此对象,并尝试将此对象添加到新的total_count
。我建议这个:
const x = raw.map(obj => {
// calculate the total
const total_count = Object.values(obj.age_range).reduce((acc, item) => acc + item.total_count, 0)
// construct the object
return { total_count, device_id: obj.device_info.device_id }
})
工作示例:
const raw = [{
"device_info": {
"name": "nokia",
"device_id": "123"
},
"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
}
}
}, {
"device_info": {
"name": "iphone",
"device_id": "456"
},
"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
}
}
}]
const x = raw.map(obj => {
const total_count = Object.values(obj.age_range).reduce((acc, item) => acc + item.total_count, 0)
return { total_count, device_id: obj.device_info.device_id }
})
console.log('x', x)
JSFiddle here。
答案 1 :(得分:0)
您应该将acc初始化为对象,并且至少应该具有密钥total_count
。
const raw = [{
"device_info": {
"name": "nokia",
"device_id": "123"
},
"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
}
}
}, {
"device_info": {
"name": "iphone",
"device_id": "456"
},
"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
}
}
}]
const x = raw.map(obj => {
return Object.values(obj.age_range)
.reduce((acc, obj2) => {
return {
total_count: acc.total_count + obj2.total_count,
device_id: obj.device_info.device_id
}
}, { total_count: 0 })
})
console.log('x', x)

答案 2 :(得分:0)
您可以切换构建对象的部分,并通过movint对象文字内部的计数来计算值。
const raw = [{ device_info: { name: "nokia", device_id: "123" }, 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 } } }, { device_info: { name: "iphone", device_id: "456" }, 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 } } }];
const x = raw.map(obj => ({
total_count: Object.values(obj.age_range).reduce((acc, obj2) => acc + obj2.total_count, 0),
device_id: obj.device_info.device_id
}));
console.log(x);