javascript
Collect books from array of objects and return collection of books as an array
let users = [{
name: 'Anna',
books: ['Bible', 'Harry Potter'],
age: 21
}, {
name: 'Bob',
books: ['War and peace', 'Romeo and Juliet'],
age: 26
}, {
name: 'Alice',
books: ['The Lord of the Rings', 'The Shining'],
age: 18
}];
let books = users.reduce((acc, curr)=> {
acc.push(...curr.books);
return acc;
}, []);
console.log(books, "books");
Output
["Bible","Harry Potter","War and peace","Romeo and Juliet","The Lord of the Rings","The Shining"]
Was this helpful?
Similar Posts
- Remove duplicates from an array and return unique values in O(n) complexity
- Finding the max/min value of an attribute in an array of objects
- Get total sum of specif attribute inside of array of objects
- Sort array of objects by key value in Javascript
- How to Filter an Array of Objects in React
- Concatenate multiple objects into one using Javascript
- Javascript Code for Sorting array and finding Largest No. in Array