我正在使用angularJS并且有一个来自API响应的JSON对象,如下所示:
var data = [
{"group": "red", "state": "running"},
{"group": "red", "state": "closed"},
{"group": "red", "state": "closed"},
{"group": "blue", "state": "running"},
{"group": "blue", "state": "running"}
];
我想在javascript函数中解析这个对象以获得以下结果:
var sumdata = [
{"group": "red", "running": 1, "closed": 2, "summary": 3},
{"group": "blue", "running": 2, "closed": 0, "summary": 2}
];
所以,我必须将名为" group"的第一个属性分组,然后计算该组中有多少个对象处于运行状态,关闭状态,并总结对象的数量。
(注: 我不想使用像LINQ.js这样的额外javascript库 )
你能帮我吗?
我尝试了以下内容,它缺少了分组,并且不知道如何将其添加到此函数中:
var getSum = function (data) {
if (!data) {
$scope.data = [];
}
else {
for (var i = 0; i < data.length; i++) {
var group = data[i][0];
var status = data[i][1];
status = (status ? status.Name : "").toUpperCase();
var running = 0;
var closed = 0;
switch (status) {
case "RUNNING":
running++;
break;
case "CLOSED":
closed++;
break;
default:
break;
}
var summary = running + closed;
$scope.dataSum.push({ "group": group, "running": running, "closed": closed, "summary": summary});
}
}
};
答案 0 :(得分:3)
这是一个带有临时对象和普通Javascript中的Array#forEach
循环的提案。
var data = [{ "group": "red", "state": "running" }, { "group": "red", "state": "closed" }, { "group": "red", "state": "closed" }, { "group": "blue", "state": "running" }, { "group": "blue", "state": "running" }],
grouped = function (array) {
var r = [];
array.forEach(function (a) {
if (!this[a.group]) {
this[a.group] = { group: a.group, running: 0, closed: 0, summary: 0 };
r.push(this[a.group]);
}
this[a.group][a.state]++;
this[a.group].summary++;
}, Object.create(null));
return r;
}(data);
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');
&#13;
答案 1 :(得分:0)
使用 LINQ 电源时,它看起来像这样:
var data = [{
"group": "red",
"state": "running"
}, {
"group": "red",
"state": "closed"
}, {
"group": "red",
"state": "closed"
}, {
"group": "blue",
"state": "running"
}, {
"group": "blue",
"state": "running"
}];
var result = Enumerable.From(data).GroupBy('$.group', null, function(key, group) {
return {
group: key,
running: group.Where(function(value) {
return value.state == 'running'
}).Count(),
closed: group.Where(function(value) {
return value.state == 'closed'
}).Count(),
summary: group.Where(function(value) {
return value.state == 'running' || value.state == 'closed'
}).Count()
}
}).ToArray()
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>
答案 2 :(得分:0)
以下剪辑适用于数据数组中的任意数量的字段。您只需提供要分组的字段的名称。它比其他示例更好,它可以用于任何类型的数据,不仅像你建议的那样命名。 希望它有所帮助。
(function(){
var data = [
{ "group": "red", "state": "running" },
{ "group": "red", "state": "closed" },
{ "group": "red", "state": "closed" },
{ "group": "blue", "state": "running" },
{ "group": "blue", "state": "running" },
{ "group": "blue", "state": "asd", "value":"33" },
{ "group": "blue", "state": "asd1", "value":"33" },
{ "group": "red", "state": "asd", "value":"33" }
],
grouped = function (array) {
var r = [];
var groupFieldName = "group";
array.forEach(function (a) {
var self = this;
if (!self[a[groupFieldName]]) {
var tempObj = {
group: a[groupFieldName]
};
self[a[groupFieldName]] = tempObj;
r.push(self[a[groupFieldName]]);
}
var keys = Object.keys(a);
keys.forEach(function(key){
if(key != groupFieldName){
if(self[a[groupFieldName]][a[key]] == undefined){
self[a[groupFieldName]][a[key]] = 1;
}else{
self[a[groupFieldName]][a[key]]++;
}
}
});
}, Object.create(null));
return r;
}(data);
console.log(JSON.stringify(grouped));
})()