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

后端 未结 3 1250
抹茶落季
抹茶落季 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:13

    A quick function to return an item with the desired property and value of that property :

    data = [{id:1,name:'Bob',hobbies:['a','b']},{id:2,name:'Alice',hobbies:['c','d']}];
    
    function getPerson(property,value){
     for(var i=0;i

    And a test :

    console.log(getPerson('name','Bob'));
    console.log(getPerson('name','Bob').hobbies);
    

提交回复
热议问题