过滤具有项目计数的唯一项目的数组

时间:2016-07-10 12:40:50

标签: javascript ecmascript-6

直升机,

我使用以下代码返回唯一的类别列表。

  stadium_cats: function () {
    let stadiums =[
        {"name":"Elland road","category":"football","country":"England"},
        {"name":"Principality stadiums","category":"rugby","country":"Wales"},
        {"name":"Twickenham","category":"rugby","country":"England"},
        {"name":"Eden Park","category":"rugby","country":"New Zealand"}
    ];

    var categories  = stadiums.map(function(obj) {return obj.category});
    categories = categories.filter(function(v,i) {return categories.indexOf(v) == i; });
    return categories;
  }

当我运行上面的内容时,我会获得一系列独特的stadium.category值。

有人可以帮我扩展它,所以不是返回一个数组,而是得到一个对象数组,如下所示:

[{"name":"football","count":"1"}{"name":"rugby","count":"3"}]

我想要这个名字以及它被展示过多少次?

这一切都可能吗?

非常感谢, 戴夫

4 个答案:

答案 0 :(得分:2)

您可以使用forEach循环和对象作为可选参数来执行此操作。

let stadiums = [{"name":"Elland road","category":"football","country":"England"},{"name":"Principality stadiums","category":"rugby","country":"Wales"},{"name":"Twickenham","category":"rugby","country":"England"},{"name":"Eden Park","category":"rugby","country":"New Zealand"}];

var result = [];
stadiums.forEach(function(e) {
  if(!this[e.category]) {
    this[e.category] = {name: e.category, count: 0}
    result.push(this[e.category]);
  }
  this[e.category].count++;
}, {});

console.log(result);

答案 1 :(得分:0)



function stadium_cats() {
  let stadiums = [{
    "name": "Elland road",
    "category": "football",
    "country": "England"
  }, {
    "name": "Principality stadiums",
    "category": "rugby",
    "country": "Wales"
  }, {
    "name": "Twickenham",
    "category": "rugby",
    "country": "England"
  }, {
    "name": "Eden Park",
    "category": "rugby",
    "country": "New Zealand"
  }];

  var cat_arr = [];
  var cats = {};
  var stadium;

  for (var i = 0; i < stadiums.length; i++) {
    stadium = stadiums[i];

    if (typeof cats[stadium.category] == 'undefined') {
      cats[stadium.category] = {
        name: stadium.category,
        count: 0
      };

      cat_arr.push(cats[stadium.category]);
    }

    cats[stadium.category].count++;
  }


  return cat_arr;
}

console.log(stadium_cats());
&#13;
&#13;
&#13;

答案 2 :(得分:0)

只有一行与ES6:

&#13;
&#13;
let stadiums = [{ name: 'Elland road', category: 'football', country: 'England' }, { name: 'Principality stadiums', category: 'rugby', country: 'Wales' }, { name: 'Twickenham', category: 'rugby', country: 'England' }, { name: 'Eden Park', category: 'rugby', country: 'New Zealand' }];
let result = [...new Set(stadiums.map(s => s.category))].map(c => ({ name: c, count: stadiums.filter(s => s.category === c).length }));

console.log(result);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

当您要求提供ES6版本时,可以使用闭包。

&#13;
&#13;
let stadiums = [{ name: "Elland road", category: "football", country: "England" }, { name: "Principality stadiums", category: "rugby", country: "Wales" }, { name: "Twickenham", category: "rugby", country: "England" }, { name: "Eden Park", category: "rugby", country: "New Zealand" }],
    result = [];

stadiums.forEach((hash => a => {
    if (!hash[a.category]) {
        hash[a.category] = { name: a.category, count: 0 };
        result.push(hash[a.category]);
    }
    hash[a.category].count++;
})(Object.create(null)));

console.log(result);
&#13;
&#13;
&#13;