var developers = [
{ name: "Joe", age: 23 ,overallLevel: "high"},
{ name: "Sue", age: 28 ,overallLevel: "advanced" },
{ name: "Jon", age: 32 ,overallLevel: "high" },
{ name: "Bob", age: 24 ,overallLevel: "high" },
{ name: "Bob", age: 20 ,overallLevel: "advanced" }
]
使用array.reduce()在所提到的数组中需要计数overallLevel [高:3,高级:2]
答案 0 :(得分:5)
你可以用对象计算它们。
{{1}}
答案 1 :(得分:1)
试试这个(你不需要array.reduce()
来做):
var
i,
count = {high: 0, advanced: 0},
developers = [
{ name: "Joe", age: 23 ,overallLevel: "high"},
{ name: "Sue", age: 28 ,overallLevel: "advanced" },
{ name: "Jon", age: 32 ,overallLevel: "high" },
{ name: "Bob", age: 24 ,overallLevel: "high" },
{ name: "Bob", age: 20 ,overallLevel: "advanced" }
];
for (i in developers) {
count[developers[i].overallLevel]++;
}
alert(JSON.stringify(count)); // Object { high=3, advanced=2}