Access object inside array which is inside an object


#1

Hi all

I have a codepen here - https://codepen.io/anon/pen/jYQYaq?editors=1112

It an array of objects, inside the objects I have another array of object.

Simple question is how do I access the object in the values array that is inside the data array of obejcts.


#2

Hi,

Something like this?

data.forEach( (dataObj) => {
  dataObj.values.forEach( (valuesObj) => {
    console.log(valuesObj);
  });
});

Produces:

// {one: 32, two: 67, three: 23}
// {one: 78, two: 90, three: 12}

#3

To access the first group of values, you can use:

console.log(data[0].values);

which gives:

[Object {
  one: 32,
  three: 23,
  two: 67
}]

and to access the first number there, you can use:

console.log(data[0].values.one); // 32

I recommend though that the values be stored in an array, as:

  {
    date: 12/1/18,
    values:[32, 67, 23]     
    }]
  },{
...

Because that way, the values are retrieved as an array, which is easier to work with.

console.log(data[0].values); // [32, 67, 23]

closed #4

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.