展平数组中的对象

flatten object inside array

我试图将第一个 key:value 对的值应用到第二个 key:value 对的数组中的每个值,同时从 books 数组中删除键,从而得到一个列表需要这个输入:

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)

您可以像这样映射 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 中的每一项,我们应用一个函数并将结果收集到一个数组中。现在,该函数实际上将另一个函数应用于它的所有书籍,结果 returns 一个数组。第二个功能(适用于所有书籍),returns当前作者,书名及其价格。

地图和地图的组合将为您解决问题

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 => {
   return [v.author, k.name, k.price];
  })
});

console.log(res);

我只是循环:

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)

这个怎么样:

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 生成具有您想要的形状的数组。