1

Say I have an array of music

The code below returns all the titles for all genres. However, I only want the title names for songs in the Country 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": ["Jazz"]
 }, {
     "title": "Curved",
     "year": 2018,
     "cast": ["Brett Shay"],
     "genres": ["Country"]
 }];

let songs = []; 

for (var i = 0; i < music.length; i++) {
 songs.push(music[i].title);
}
    console.log(songs);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Have a look into [`[].filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). – Sirko Nov 14 '19 at 06:21

2 Answers2

4

.filter the array by whether Country is included in the genres, then .map to the titles:

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:["Jazz"]},{title:"Curved",year:2018,cast:["Brett Shay"],genres:["Country"]}];
 
 const countryTitles = music
   .filter(({ genres }) => genres.includes('Country'))
   .map(({ title }) => title);
 console.log(countryTitles)

If you want to do it while only iterating over the dataset once, use reduce instead:

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:["Jazz"]},{title:"Curved",year:2018,cast:["Brett Shay"],genres:["Country"]}];
 
const countryTitles = music
  .reduce((a, { genres, title }) => {
     if (genres.includes('Country')) {
       a.push(title)
     }
     return a;
   }, []);
console.log(countryTitles)
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Try this:

let songs = []; 

for (var i = 0; i < music.length; i++) {
    if(music[i].genres == "Country"){ // if music genres equal to "country"
        songs.push(music[i].title);
    }  
}

// output : Cheats,Road,Mountain Curve,Curved