首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将数组中的元素分组为满足特定条件的对象,并将其推送到新数组中

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

Stack Overflow用户
提问于 2017-03-21 20:23:14
回答 4查看 36关注 0票数 0

我有以下数组:

代码语言:javascript
复制
"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,这样我就可以得到如下内容:

代码语言:javascript
复制
[
   {
    "label": "Windows",
    "value": "9999"
   },
   {
    "label": "Android",
    "value": "8888"
   },
   {
    "label": "iOS",
    "value": "7777"
   },
   {
    "label": "Macos",
    "value": "10000"
   },
   {
    "label": "Other",
    "value": "5000"
   }
]

我在这里完全迷路了,我非常感谢你的帮助和指导。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-03-21 20:48:46

在您的场景中使用Array.prototype.reduceString.prototype.match。希望这能有所帮助。

代码语言:javascript
复制
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);

票数 1
EN

Stack Overflow用户

发布于 2017-03-21 20:32:39

  • 创建一个regex值数组,每个值都匹配特定的操作系统,如.*windows.*
  • 迭代数据数组,尝试将每个条目的_id与regex匹配。
  • 如果匹配,则在输出数组中向value添加计数值。如果它不是一个匹配,那么添加到“其他”。

你怎么做是由你自己决定的,这只是一个一般性的想法。

票数 0
EN

Stack Overflow用户

发布于 2017-03-21 20:36:44

你可以这样做:

代码语言:javascript
复制
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;

这将迭代设备数组,并在数组中找到匹配的元素,并将计数添加到结果数组中该项的值中。如果找不到该项,或者它是第一个项,则会将一个新对象推送到结果数组。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42937449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档