I want to take the value of the first key:value pair and apply it to each value in the array of the second key:value pair, all while removing the keys from the books array. This process will result in a list that looks like this:
var fictionCatalog = [
{
author: 'Michael Crichton',// push into each book
books: [
{name: 'Sphere', price: 10.99},
{name: 'Jurassic Park', price: 5.99},
{name: 'The Andromeda Strain', price: 9.99},
{name: 'Prey', price: 5.99}
]
}
]
The desired output should be:
[
[ Michael Crichton, 'Sphere', 10.99 ],
[ Michael Crichton, 'Jurassic Park', 5.99 ],
[ Michael Crichton, 'The Andromeda Strain', 9.99 ],
[ Michael Crichton, 'Prey', 5.99 ]
]
However, I am facing difficulties with the following code snippet:
var fictionCatalog = [
{
author: 'Michael Crichton',
books: [
{name: 'Sphere', price: 10.99},
{name: 'Jurassic Park', price: 5.99},
{name: 'The Andromeda Strain', price: 9.99},
{name: 'Prey', price: 5.99}
]
}
]
var collection = fictionCatalog.reduce(function(prev, curr) {
return prev.concat(curr.author, curr.books);
}, []);
console.log(collection)