我有以下数组:
"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
对应于同一操作系统的所有值分组,并添加每个元素的 count
,这样我可以获得类似以下内容:
[
{
"label": "Windows",
"value": "9999"
},
{
"label": "Android",
"value": "8888"
},
{
"label": "iOS",
"value": "7777"
},
{
"label": "Macos",
"value": "10000"
},
{
"label": "Other",
"value": "5000"
}
]
我完全迷失了方向,我非常感谢一些帮助和指导。
最佳答案
针对您的场景使用了 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);
关于javascript - 如何将数组中的元素分组为满足特定条件的对象并将其推送到新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42937449/