How to access specific value from a nested array within an object array?

后端 未结 3 1224
抹茶落季
抹茶落季 2021-01-27 17:35

I am trying to get a specific field value from a nested array within an object array. I\'m assuming I\'d use map, but every time I use it in this way I get two empty arrays nest

3条回答
  •  无人共我
    2021-01-27 18:20

    var joe = parentArray.find(function (item) {
        return item.name === 'Joe';
    });
    
    if (joe) {
        var joesHobbiesNames = joe.hobbies.map(function (hobbie) {
           return hobbie.name;
        });
    }
    

    Or in ES6

    var joe = parentArray.find((item) => item.name === 'Joe');
    
    if (joe) {
        var joesHobbiesNames = joe.hobbies.map((hobbie) => hobbie.name);
    }
    

提交回复
热议问题