-3

How would I go about returning an object that returns the genres of Country, Rock, and Pop with and a count of how many songs are in each genre? The output would look something like:

Country: 4, Rock: 2, Pop: 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"]
 }];

This is my code. I am getting all the genres and no counts.

let songs = []; 

for (var i = 0; i < music.length; i++) {
    songs.push(music[i].genres);
}
    console.log(songs);
  • 1
    Please don't post some code just for the sake of it. The code you've posted here is same as you posted for your [previous question](https://stackoverflow.com/q/58850514/3082296). – adiga Nov 14 '19 at 07:52
  • @adiga sorry i have just been stuck on it for quite some time, i've tried a couple of different things but this code is the closest i have gotten to a valid way of doing it. – huntermaire Nov 14 '19 at 07:56
  • There are plenty of question which are similar to the one you've asked (Eg: [group array and get count](https://stackoverflow.com/questions/52711740)). The approach is similar. – adiga Nov 14 '19 at 07:57
  • @adiga ok thank you very much – huntermaire Nov 14 '19 at 07:59

4 Answers4

0

The reduce method can count all songs by genre:

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);
StepUp
  • 36,391
  • 15
  • 88
  • 148
0

You can use a whitelist in the form of a set, and reduce each object, and each genre inside that object into a count of each song's genre:

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)
Kobe
  • 6,226
  • 1
  • 14
  • 35
0

Try this man , It's work for me

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);
-1

You should make a function like my getGenre function:

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)
StackSlave
  • 10,613
  • 2
  • 18
  • 35