2016-12-28 3 views
7

Ich versuche, den Wert des ersten Schlüssel: Wert-Paar auf jeden Wert im Array des zweiten Schlüssel: Wert-Paar beim Entfernen der Schlüssel aus dem Buch-Array, was zu einer Liste, die dauert dieser Eingang:flattern Objekt innerhalb Array

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

und diesen Ausgang log:

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

Wo stecken

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)

Antwort

5

Sie das Ergebnis books Karte kann wie dieser

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

console.log(collection); 

Ausgabe

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

Für jedes der Elemente in der fictionCatalog, wenden wir eine Funktion und sammeln die Ergebnisse in einem Array . Diese Funktion wendet nun eine andere Funktion auf alle ihre Bücher an und gibt als Ergebnis ein Array zurück. Die zweite Funktion (für alle Bücher) gibt den aktuellen Autor, den Buchnamen und den Preis zurück.

+0

Sie würden eine Art "concatMap" anstelle von "map" benötigen. – Bergi

2

Eine Kombination von Karte und Karte wird für Sie den Trick

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

Ich hatte gerade Schleife:

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

Wie wäre es Dies :

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 ]; 
           })); 
}, []); 

Es verwendet reduce und map das Array mit der Form, die Sie wollen, zu generieren.

Verwandte Themen