This question is quite old, so as a contemporary update. With the onset of ES2015 there are alternatives to get a hold of the data you require. There is now a feature called object destructingdestructuring for accessing nested objects.
const data = {
code: 42,
items: [{
id: 1,
name: 'foo'
}, {
id: 2,
name: 'bar'
}]
};
const {
items: [, {
name: secondName
}]
} = data;
console.log(secondName);
The above example creates a variable called secondName
from the name
key from an array called items
, the lonely ,
says skip the first object in the array.
Notably it's probably overkill for this example, as simple array acccess is easier to read, but it comes in useful when breaking apart objects in general.
This is very brief intro to your specific use case, destructingdestructuring can be an unusual syntax to get used to at first. I'd recommend reading Mozilla's DestructingDestructuring Assignment documentation to learn more.