2016-12-28 53 views
7

我試圖將第一個鍵:值對的值應用於第二個鍵:數組對中的每個值,同時從書籍數組中刪除鍵,從而生成一個列表此輸入:平面陣列中的對象

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)

回答

5

可以映射的books結果這樣

var collection = fictionCatalog.map(function(obj) { 
    return obj.books.map(function(book) { 
    return [obj.author, book.name, book.price]; 
    }); 
}); 

console.log(collection); 

輸出

[ [ [ 'Michael Crichton', 'Sphere', 10.99 ], 
    [ 'Michael Crichton', 'Jurassic Park', 5.99 ], 
    [ 'Michael Crichton', 'The Andromeda Strain', 9.99 ], 
    [ 'Michael Crichton', 'Prey', 5.99 ] ] ] 

對於每個項目的fictionCatalog,我們在一個陣列應用功能,並收集結果。現在,該函數實際上將其他函數應用於其所有書籍並作爲結果返回數組。第二個功能(適用於所有書籍)返回當前作者,書名和價格。

+0

雖然你需要某種'concatMap'而不是'map'。 – Bergi

2

地圖和地圖的組合就可以了你

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} 
 
    ] 
 
    } 
 
]; 
 

 
var res = fictionCatalog.map(v => { 
 
    return v.books.map(k => { 
 
    \t return [v.author, k.name, k.price]; 
 
    }) 
 
}); 
 

 
console.log(res);

1

,我正循環:

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 = []; 
 

 
for (var a = 0; a < fictionCatalog.length; a++) { 
 
    var author = fictionCatalog[a].author; 
 
    for (var b = 0; b < fictionCatalog[a].books.length; b++) { 
 
    collection.push([ 
 
     author, 
 
     fictionCatalog[a].books[b].name, 
 
     fictionCatalog[a].books[b].price 
 
    ]); 
 
    } 
 
} 
 

 
console.log(collection)

0

怎麼樣這個:

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.books.map( 
           function(book) { 
           return [ curr.author, book.name, book.price ]; 
           })); 
}, []); 

它採用reducemap生成你想要的形狀排列。