我試圖將第一個鍵:值對的值應用於第二個鍵:數組對中的每個值,同時從書籍數組中刪除鍵,從而生成一個列表此輸入:平面陣列中的對象
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}
]
}
]
和日誌的輸出:
[
[ Michael Crichton, 'Sphere', 10.99 ],
[ Michael Crichton, 'Jurassic Park', 5.99 ],
[ Michael Crichton, 'The Andromeda Strain', 9.99 ],
[ Michael Crichton, 'Prey', 5.99 ]
]
我卡住
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)
雖然你需要某種'concatMap'而不是'map'。 – Bergi