【问题标题】:How to return an array of objects with a count of how many for each one [duplicate]如何返回一个对象数组,计算每个对象的数量[重复]
【发布时间】:2020-03-10 02:48:26
【问题描述】:

我将如何返回一个返回 Country、Rock 和 Pop 流派的对象,并计算每种流派的歌曲数量?输出看起来像:

国家:4, 摇滚:2, 流行:1

const music=  [{
     "title": "Cheats",
     "year": 2018,
     "cast": ["Jane Rhee", "Kacey Brown"],
     "genres": ["Country"]
 }, {
     "title": "Road",
     "year": 2018,
     "cast": ["Jeff Bates", "Alan Walker", "Cindy Bates"],
     "genres": ["Country"]
 }, {
     "title": "Trail Down",
     "year": 2018,
     "cast": ["Ken Clemont"],
     "genres": ["Jazz"]
 }, {
     "title": "Way Down",
     "year": 2018,
     "cast": ["Denzel Harr", "Dan Smith", "Lee Kyle", "Nate Hill"],
     "genres": ["Pop"]
 }, {
     "title": "Fountain",
     "year": 2018,
     "cast": ["Brad Smith", "Rosa King"],
     "genres": ["Rock"]
 }, {
     "title": "Gold Bells",
     "year": 2018,
     "cast": ["Paisley John"],
     "genres": ["Blues"]
 }, {
     "title": "Mountain Curve",
     "year": 2018,
     "cast": ["Michael Johnson"],
     "genres": ["Country"]
 }, {
     "title": "Arabella",
     "year": 2018,
     "cast": [],
     "genres": ["Rock"]
 }, {
     "title": "Curved",
     "year": 2018,
     "cast": ["Brett Shay"],
     "genres": ["Country"]
 }];

这是我的代码。我得到了所有的流派,没有计数。

let songs = []; 

for (var i = 0; i < music.length; i++) {
    songs.push(music[i].genres);
}
    console.log(songs);

【问题讨论】:

  • 请不要仅仅为了它而发布一些代码。您在此处发布的代码与您为 previous question 发布的代码相同。
  • @adiga 抱歉,我已经坚持了很长一段时间,我尝试了几种不同的方法,但这段代码是我最接近有效的方法。
  • 有很多问题与您提出的问题相似(例如:group array and get count)。方法类似。
  • @adiga 好的,非常感谢

标签: javascript arrays json filter


【解决方案1】:

reduce 方法可以按流派统计所有歌曲:

const music = [{
    "title": "Cheats", "year": 2018,
    "cast": ["Jane Rhee", "Kacey Brown"], "genres": ["Country"]
}, {
    "title": "Road", "year": 2018,
    "cast": ["Jeff Bates", "Alan Walker", "Cindy Bates"], "genres": ["Country"]
}, {
    "title": "Trail Down", "year": 2018,
    "cast": ["Ken Clemont"], "genres": ["Jazz"]
}, {
    "title": "Way Down", "year": 2018,
    "cast": ["Denzel Harr", "Dan Smith", "Lee Kyle", "Nate Hill"], "genres": ["Pop"]
}, {
    "title": "Fountain", "year": 2018,
    "cast": ["Brad Smith", "Rosa King"], "genres": ["Rock"]
}, {
    "title": "Gold Bells", "year": 2018,
    "cast": ["Paisley John"],
    "genres": ["Blues"]
}, {
    "title": "Mountain Curve", "year": 2018,
    "cast": ["Michael Johnson"], "genres": ["Country"]
}, {
    "title": "Arabella", "year": 2018,
    "cast": [], "genres": ["Rock"]
}, {
    "title": "Curved", "year": 2018,
    "cast": ["Brett Shay"], "genres": ["Country"]
}];

const result = music.reduce((a, {genres}) => {
    a[genres] = a[genres] || {songCount: 0};
    a[genres].songCount += 1
    return a;
}, {});
console.log(result);

【讨论】:

  • 没有摇滚歌曲?没用!
  • @StackSlave 哎呀,感谢您的关注。我已经编辑了我的回复。请看我更新的答案
【解决方案2】:

您可以使用集合形式的白名单,并将每个对象以及该对象内的每个流派减少为每首歌曲流派的计数:

const music = [
  { title: 'Cheats', year: 2018, cast: ['Jane Rhee', 'Kacey Brown'], genres: ['Country'] },
  { title: 'Road', year: 2018, cast: ['Jeff Bates', 'Alan Walker', 'Cindy Bates'], genres: ['Country'] },
  { title: 'Trail Down', year: 2018, cast: ['Ken Clemont'], genres: ['Jazz'] },
  { title: 'Way Down', year: 2018, cast: ['Denzel Harr', 'Dan Smith', 'Lee Kyle', 'Nate Hill'], genres: ['Pop'] },
  { title: 'Fountain', year: 2018, cast: ['Brad Smith', 'Rosa King'], genres: ['Rock'] },
  { title: 'Gold Bells', year: 2018, cast: ['Paisley John'], genres: ['Blues'] },
  { title: 'Mountain Curve', year: 2018, cast: ['Michael Johnson'], genres: ['Country'] },
  { title: 'Arabella', year: 2018, cast: [], genres: ['Rock'] },
  { title: 'Curved', year: 2018, cast: ['Brett Shay'], genres: ['Country'] }
]

const set = new Set(['Country', 'Rock', 'Pop'])
const count = music.reduce((a, o) => (o.genres.forEach(g => (set.has(g) ? (a[g] = (a[g] || 0) + 1) : null)), a), {})

console.log(count)

【讨论】:

    【解决方案3】:

    试试这个人,这对我有用

    const music=  [{
         "title": "Cheats",
         "year": 2018,
         "cast": ["Jane Rhee", "Kacey Brown"],
         "genres": ["Country"]
     }, {
         "title": "Road",
         "year": 2018,
         "cast": ["Jeff Bates", "Alan Walker", "Cindy Bates"],
         "genres": ["Country"]
     }, {
         "title": "Trail Down",
         "year": 2018,
         "cast": ["Ken Clemont"],
         "genres": ["Jazz"]
     }, {
         "title": "Way Down",
         "year": 2018,
         "cast": ["Denzel Harr", "Dan Smith", "Lee Kyle", "Nate Hill"],
         "genres": ["Pop"]
     }, {
         "title": "Fountain",
         "year": 2018,
         "cast": ["Brad Smith", "Rosa King"],
         "genres": ["Rock"]
     }, {
         "title": "Gold Bells",
         "year": 2018,
         "cast": ["Paisley John"],
         "genres": ["Blues"]
     }, {
         "title": "Mountain Curve",
         "year": 2018,
         "cast": ["Michael Johnson"],
         "genres": ["Country"]
     }, {
         "title": "Arabella",
         "year": 2018,
         "cast": [],
         "genres": ["Rock"]
     }, {
         "title": "Curved",
         "year": 2018,
         "cast": ["Brett Shay"],
         "genres": ["Country"]
     }];
        var res = {}; 
        music.forEach(function(v) {
            res[v.genres] = (res[v.genres] || 0) + 1; 
        });
        console.log("Country" + " " + res.Country+ " " + "Rock" + " " + res.Rock + " " + "Pop" + " " + res.Pop);

    【讨论】:

      【解决方案4】:

      你应该做一个像我的getGenre函数一样的函数:

      const music =  [{
           "title": "Cheats",
           "year": 2018,
           "cast": ["Jane Rhee", "Kacey Brown"],
           "genres": ["Country"]
       }, {
           "title": "Road",
           "year": 2018,
           "cast": ["Jeff Bates", "Alan Walker", "Cindy Bates"],
           "genres": ["Country"]
       }, {
           "title": "Trail Down",
           "year": 2018,
           "cast": ["Ken Clemont"],
           "genres": ["Jazz"]
       }, {
           "title": "Way Down",
           "year": 2018,
           "cast": ["Denzel Harr", "Dan Smith", "Lee Kyle", "Nate Hill"],
           "genres": ["Pop"]
       }, {
           "title": "Fountain",
           "year": 2018,
           "cast": ["Brad Smith", "Rosa King"],
           "genres": ["Rock"]
       }, {
           "title": "Gold Bells",
           "year": 2018,
           "cast": ["Paisley John"],
           "genres": ["Blues"]
       }, {
           "title": "Mountain Curve",
           "year": 2018,
           "cast": ["Michael Johnson"],
           "genres": ["Country"]
       }, {
           "title": "Arabella",
           "year": 2018,
           "cast": [],
           "genres": ["Rock"]
       }, {
           "title": "Curved",
           "year": 2018,
           "cast": ["Brett Shay"],
           "genres": ["Country"]
      }];
      function getGenre(genre){
        return music.filter(function(o){
          return o.genres.indexOf(genre) !== -1;
        });
      }
      /* or
      function getGenre(genre){
         return music.filter(o => o.genres.indexOf(genre) !== -1);
      }
      // other is more backward compatible
      */
      var country = getGenre('Country'), pop = getGenre('Pop'), rock = getGenre('Rock');
      console.log(country); console.log('Country Song Total = '+country.length);
      console.log(pop); console.log('Pop Song Total = '+pop.length);
      console.log(rock); console.log('Rock Song Total = '+rock.length)

      【讨论】:

        猜你喜欢
        • 2021-11-29
        • 2019-01-17
        • 1970-01-01
        • 1970-01-01
        • 2020-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        相关资源
        最近更新 更多