【问题标题】:How to transform an array/dataset in javascript? [duplicate]如何在javascript中转换数组/数据集? [复制]
【发布时间】:2019-11-14 06:19:08
【问题描述】:

假设我有一系列音乐

下面的代码返回所有类型的所有标题。但是,我只想要 Country 流派歌曲的标题名称。

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

【问题讨论】:

标签: javascript arrays json


【解决方案1】:

.filter 数组,根据Country 是否包含在流派中,然后.map 到标题:

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)

如果您只想在数据集上迭代一次,请改用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:["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)

【讨论】:

    【解决方案2】:

    试试这个:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-01
      • 2016-03-02
      • 2011-07-06
      • 2011-11-27
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多