javascript - 如何将数组中的元素分组为满足特定条件的对象并将其推送到新数组

标签 javascript arrays object

我有以下数组:

"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/

相关文章:

javascript - iOS UIWebView html 生成器?

javascript - Canvas 绘制很慢

javascript - 替换对象(和/或数组)中字符串的所有实例 - JavaScript

javascript - 这段代码有什么作用? "data.target == this"

r - 将函数结果存储在 R 中的复杂对象 (?) 中

javascript - 附加文本立即消失

javascript - 单击文本区域隐藏标签值

android - 检查 json 对象是否包含 json 数组?

javascript - 如何从数组中检查

php - 使用 php 和 sql 上传多张图片