Reputation: 211
Below is the json data that'd I'd like destructure and extract just the title values.
[{
"id": 1,
"title": "Python Crash Course",
"author": "Eric Matthes",
"quantity": 5
},
{
"id": 2,
"title": "Head-First Python",
"author": "Paul Barry",
"quantity": 2
}
]
Upvotes: -1
Views: 1753
Reputation: 439
If this is an array of objects and it's in the file (not as an external file) then,
const arrayObj = [
{
"id": 1,
"title": "Head-First Python",
"author": "Paul Barry",
"quantity": 1
},
...
]
const {title} = arrayObj[0]
console.log(title) //Head-First Python
That will give you the value of one object in the array. To get all the 'title' properties you could loop through the array.
arrayObj.forEach(el => {
let {title} = el
})
Upvotes: 1
Reputation: 3111
Getting specific properties of objects inside an array does not need destructuring. This can be done with Array.prototype.map:
jsonArray.map(object => object.title);
// results in ["Python Crash Course", "Head-First Python"]
If you really want to use destructuring, you could change the function provided to map
to only select the title:
jsonArray.map(({title}) => title);
// results in ["Python Crash Course", "Head-First Python"]
Upvotes: 1