【发布时间】: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