0

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 nested inside two empty objects. I know this is wrong, I'm just showing where my thinking process is going.

function getChildArray(item, index) {
   var x = [item.hobbies]
      return x
}

console.log(parentArray.map(getChildArray))

This is an example of my document structure:

[  
   {  
      "id":12345678900,
      "name":"Jasmin",
      "age":27,
      "hobbies":[  
         {  
            "id":1221,
            "name":"hiking",
            "when":"anytime"
         },
         {  
            "id":9865,
            "name":"eating",
            "when":"all the time"
         }
      ]
   },
   {  
      "id":223456789001,
      "name":"Joe",
      "age":35,
      "hobbies":[  
         {  
            "id":989,
            "name":"gaming",
            "when":"anytime"
         },
         {  
            "id":2355,
            "name":"online gaming",
            "when":"all the time"
         }
      ]
   }
]

How would I, for example, be able to retrieve a list of Joe's hobbies by name only?

4
  • There are syntax errors at JSON Commented Jul 3, 2016 at 4:24
  • He forgot the comma after "name: 'Joe'" Commented Jul 3, 2016 at 4:29
  • @developer033 See also hiking, eating, comma following 989, online gaming Commented Jul 3, 2016 at 4:31
  • Sorry for the human error! Fixed it now. Commented Jul 3, 2016 at 4:34

3 Answers 3

2
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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I recommend you to use the filter method instead of find, because if there are several obects with name property "Joe", the find method will return only one object instead of multiple.
It was intended to use find instead of filter. The OP asked for JOE's hobbies, not for all the hobbies of all the people called Joe
1

Since array.find is not available in all browsers yet, and you may not be using a build tool, here is a complete ES5 way. It uses filter and map:

var data = [{ id: 12345678900, name: 'Jasmin', age: 27, hobbies: [{'id': 1221, 'name': 'hiking', 'when': 'anytime'}, { 'id': 9865, 'name': 'eating', 'when': 'all the time' }] }, { id: 223456789001, name: 'Joe', age: 35, hobbies: [{'id': 989, 'name':
'gaming', 'when': 'anytime'}, { 'id': 2355, 'name': 'online gaming', 'when': 'all the time' }]}];


function getHobbiesByName(name) {
  return data.filter(function(person) {
    return (person.name == name);
  })[0].hobbies.map(function(hobby) {
    return hobby.name
  })
}

console.log(getHobbiesByName('Joe'))

Comments

0

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<data.length;i++) if(data[i][property] == value) return data[i];
 return {};
}

And a test :

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

1 Comment

It returns undefined - also this is just a test, real use would call for checking if the person exists anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.