我有以下JSON数组,我想创建对象表单状态键计数
[
{
"id": "65:0",
"status": "ORANGE"
},
{
"id": "65:0",
"status": "GREEN"
},
{
"id": "65:0",
"status": "ORANGE"
},
{
"id": "65:0",
"status": "YELLOW"
},
{
"id": "65:0",
"status": "RED"
},
{
"id": "65:0",
"status": "GREEN"
},
{
"id": "65:0",
"status": "GREEN"
},
{
"id": "65:0",
"status": "ORANGE"
},
{
"id": "65:0",
"status": "YELLOW"
},
{
"id": "65:0",
"status": "GREEN"
}
]
想要计算状态键值并创建以下对象
{
'ORANGE' : 3,
'GREEN' : 4,
'YELLOW' : 2,
'RED' : 1,
}
答案 0 :(得分:6)
使用 Array#reduce
方法
var res = data.reduce(function(obj, v) {
// increment or set the property
// `(obj[v.status] || 0)` returns the property value if defined
// or 0 ( since `undefined` is a falsy value
obj[v.status] = (obj[v.status] || 0) + 1;
// return the updated object
return obj;
// set the initial value as an object
}, {})
var data = [{
"id": "65:0",
"status": "ORANGE"
}, {
"id": "65:0",
"status": "GREEN"
}, {
"id": "65:0",
"status": "ORANGE"
}, {
"id": "65:0",
"status": "YELLOW"
}, {
"id": "65:0",
"status": "RED"
}, {
"id": "65:0",
"status": "GREEN"
}, {
"id": "65:0",
"status": "GREEN"
}, {
"id": "65:0",
"status": "ORANGE"
}, {
"id": "65:0",
"status": "YELLOW"
}, {
"id": "65:0",
"status": "GREEN"
}];
var res = data.reduce(function(obj, v) {
obj[v.status] = (obj[v.status] || 0) + 1;
return obj;
}, {})
console.log(res);
虽然您可以使用相同代码的 Array#forEach
方法。
var res = {};
data.forEach(function(v) {
res[v.status] = (res[v.status] || 0) + 1;
})
var data = [{
"id": "65:0",
"status": "ORANGE"
}, {
"id": "65:0",
"status": "GREEN"
}, {
"id": "65:0",
"status": "ORANGE"
}, {
"id": "65:0",
"status": "YELLOW"
}, {
"id": "65:0",
"status": "RED"
}, {
"id": "65:0",
"status": "GREEN"
}, {
"id": "65:0",
"status": "GREEN"
}, {
"id": "65:0",
"status": "ORANGE"
}, {
"id": "65:0",
"status": "YELLOW"
}, {
"id": "65:0",
"status": "GREEN"
}];
var res = {};
data.forEach(function(v) {
res[v.status] = (res[v.status] || 0) + 1;
})
console.log(res);
答案 1 :(得分:1)
var obj = [
{
"id": "65:0",
"status": "ORANGE"
},
{
"id": "65:0",
"name": "BIU",
"status": "GREEN"
},
{
"id": "65:0",
"name": "BIU",
"status": "ORANGE"
},
{
"id": "65:0",
"status": "YELLOW"
},
{
"id": "65:0",
"status": "RED"
},
{
"id": "65:0",
"status": "GREEN"
},
{
"id": "65:0",
"status": "GREEN"
},
{
"id": "65:0",
"status": "ORANGE"
},
{
"id": "65:0",
"status": "YELLOW"
},
{
"id": "65:0",
"status": "GREEN"
}
] ;
var rez={};
obj.forEach(function(item){
rez[item.status] ? rez[item.status]++ : rez[item.status] = 1;
});
console.log(rez);
答案 2 :(得分:1)
你想要这种输出格式吗?
var json=[
{
"id": "65:0",
"status": "ORANGE"
},
{
"id": "65:0",
"status": "GREEN"
},
{
"id": "65:0",
"status": "ORANGE"
},
{
"id": "65:0",
"status": "YELLOW"
},
{
"id": "65:0",
"status": "RED"
},
{
"id": "65:0",
"status": "GREEN"
},
{
"id": "65:0",
"status": "GREEN"
},
{
"id": "65:0",
"status": "ORANGE"
},
{
"id": "65:0",
"status": "YELLOW"
},
{
"id": "65:0",
"status": "GREEN"
}
] ;
var obj={};
json.forEach(function(item){
obj[item.status]? obj[item.status]++ : obj[item.status]=1;
});
console.log(obj)