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

时间:2017-03-21 20:23:14

标签: 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"
   }
]

我完全迷失在这里,我真的很感激一些帮助和指导。

4 个答案:

答案 0 :(得分:1)

为您的方案使用了Array.prototype.reduceString.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);

答案 1 :(得分:0)

  • 创建一个正则表达式数组,每个值都匹配特定的操作系统,如.*windows.*
  • 迭代您的数据阵列,尝试将每个条目_id与正则表达式匹配。
  • 如果匹配,则将计数值添加到输出数组中的value。如果不匹配,请添加到"其他"。

你是如何做的取决于你,这只是一个普遍的想法。

答案 2 :(得分:0)

您可以执行以下操作:

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;

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

答案 3 :(得分:0)

您可以将数组与分组字符串一起使用,并使用哈希表进行计数。

var data = { 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 }] },
    os = ["Windows", "Android", "iOS", "Mac OS", "Other"],
    result = data.devices.reduce(function (hash) {
        return function (r, a) {
            var key = 'Other';
            os.some(function (o) {
                if (typeof a._id === 'string' && a._id.toLowerCase().indexOf(o.toLowerCase()) !== -1) {
                    key = o;
                    return true;
                }
            });
            if (!hash[key]) {
                hash[key] = { label: key, value: 0 };
                r.push(hash[key]);
            }
            hash[key].value += a.count;
            return r;
        };
    }(Object.create(null)), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }