我有以下数组:
"devices": [
{
"_id": "HP Printer",
"count": 1
},
{
"_id": "55UA8990",
"count": 1
},
{
"_id": "Mac OS X 10.5",
"count": 1
},
{
"_id": "Nokia",
"count": 4
},
{
"_id": "Ubuntu Linux",
"count": 3
},
{
"_id": "LG handset",
"count": 1
},
{
"_id": "Samsung Home Audio/Video equipment",
"count": 1
},
{
"_id": "Generic Linux",
"count": 1
},
{
"_id": "Sony handset",
"count": 1
},
{
"_id": "Mac OS X 10.8",
"count": 12
},
{
"_id": "Linux Handheld",
"count": 1
},
{
"_id": "Symbian OS",
"count": 15
},
{
"_id": "RIM BlackBerry",
"count": 28
},
{
"_id": "Apple iPod",
"count": 1
},
{
"_id": "LG BL40",
"count": 1
},
{
"_id": "Slingbox",
"count": 67
},
{
"_id": "Windows XP",
"count": 60
},
{
"_id": "BlackBerry",
"count": 51
},
{
"_id": "Chrome OS",
"count": 7
},
{
"_id": "Eye-Fi Wireless Memory Card",
"count": 6
},
{
"_id": "Mac OS X 10.9",
"count": 62
},
{
"_id": "Mac OS X 10.10",
"count": 215
},
{
"_id": "Windows Vista",
"count": 4
},
{
"_id": "Windows Mobile OS",
"count": 213
},
{
"_id": "Windows 7/Vista",
"count": 394
},
{
"_id": "Mac OS X 10.11",
"count": 303
},
{
"_id": "Samsung TV",
"count": 1
},
{
"_id": "PlayStation Portable",
"count": 1
},
{
"_id": "Mac OS X",
"count": 755
},
{
"_id": "Windows 7",
"count": 612
},
{
"_id": "Apple iPad",
"count": 2597
},
{
"_id": "Ellipsis 8",
"count": 193
},
{
"_id": "Mac OS X 10.12",
"count": 550
},
{
"_id": "Nexus",
"count": 4657
},
{
"_id": "Windows 10",
"count": 772
},
{
"_id": "Windows 8",
"count": 1191
},
{
"_id": "Xbox 360",
"count": 39
},
{
"_id": "Mac OS X 10.6",
"count": 5
},
{
"_id": "Apple iPhone",
"count": 41039
},
{
"_id": "iOS",
"count": 25725
},
{
"_id": "Debian-based Linux",
"count": 9
},
{
"_id": null,
"count": 5291
},
{
"_id": "Mac OS X 10.7",
"count": 16
},
{
"_id": "Belkin Wireless Router",
"count": 1
},
{
"_id": "Windows",
"count": 1002
},
{
"_id": "Android",
"count": 51314
}
]我试图将_id对应于相同os的所有值分组,并从每个元素中添加count,这样我就可以得到如下内容:
[
{
"label": "Windows",
"value": "9999"
},
{
"label": "Android",
"value": "8888"
},
{
"label": "iOS",
"value": "7777"
},
{
"label": "Macos",
"value": "10000"
},
{
"label": "Other",
"value": "5000"
}
]我在这里完全迷路了,我非常感谢你的帮助和指导。
发布于 2017-03-21 20:48:46
在您的场景中使用Array.prototype.reduce和String.prototype.match。希望这能有所帮助。
var input = {"devices":[{"_id":"HP Printer","count":1},{"_id":"55UA8990","count":1},{"_id":"Mac OS X 10.5","count":1},{"_id":"Nokia","count":4},{"_id":"Ubuntu Linux","count":3},{"_id":"LG handset","count":1},{"_id":"Samsung Home Audio/Video equipment","count":1},{"_id":"Generic Linux","count":1},{"_id":"Sony handset","count":1},{"_id":"Mac OS X 10.8","count":12},{"_id":"Linux Handheld","count":1},{"_id":"Symbian OS","count":15},{"_id":"RIM BlackBerry","count":28},{"_id":"Apple iPod","count":1},{"_id":"LG BL40","count":1},{"_id":"Slingbox","count":67},{"_id":"Windows XP","count":60},{"_id":"BlackBerry","count":51},{"_id":"Chrome OS","count":7},{"_id":"Eye-Fi Wireless Memory Card","count":6},{"_id":"Mac OS X 10.9","count":62},{"_id":"Mac OS X 10.10","count":215},{"_id":"Windows Vista","count":4},{"_id":"Windows Mobile OS","count":213},{"_id":"Windows 7/Vista","count":394},{"_id":"Mac OS X 10.11","count":303},{"_id":"Samsung TV","count":1},{"_id":"PlayStation Portable","count":1},{"_id":"Mac OS X","count":755},{"_id":"Windows 7","count":612},{"_id":"Apple iPad","count":2597},{"_id":"Ellipsis 8","count":193},{"_id":"Mac OS X 10.12","count":550},{"_id":"Nexus","count":4657},{"_id":"Windows 10","count":772},{"_id":"Windows 8","count":1191},{"_id":"Xbox 360","count":39},{"_id":"Mac OS X 10.6","count":5},{"_id":"Apple iPhone","count":41039},{"_id":"iOS","count":25725},{"_id":"Debian-based Linux","count":9},{"_id":null,"count":5291},{"_id":"Mac OS X 10.7","count":16},{"_id":"Belkin Wireless Router","count":1},{"_id":"Windows","count":1002},{"_id":"Android","count":51314}]};
var output = input.devices.reduce(function(a, b) {
var match = b._id?b._id.match(/(Mac|Windows|Android|iOS)/):null;
match = match ? match[0] : 'Others';
a[match] = (a[match] || 0) + b.count;
return a;
}, {});
output = Object.keys(output).map(function(k){
return {
label: k,
value: output[k]
};
});
console.log(output);
发布于 2017-03-21 20:32:39
.*windows.*。_id与regex匹配。value添加计数值。如果它不是一个匹配,那么添加到“其他”。你怎么做是由你自己决定的,这只是一个一般性的想法。
发布于 2017-03-21 20:36:44
你可以这样做:
var results = [];
devices.forEach(function(d) {
if(results.length === 0){ //shortcut for the first element
results.push({label:d._id, value: d.count});
} else {
var found = false;
for(var j =0; j < results.length; j++){
var i = results[j];
if(i.label === d._id){
found = true;
i.value += d.count;
results[j] = i;
}
});
if(!found) {
results.push({label:d._id, value: d.count});
}
}
});
return results;这将迭代设备数组,并在数组中找到匹配的元素,并将计数添加到结果数组中该项的值中。如果找不到该项,或者它是第一个项,则会将一个新对象推送到结果数组。
https://stackoverflow.com/questions/42937449
复制相似问题