在数组内展平对象

时间:2016-12-28 18:32:30

标签: javascript arrays flatten

我正在尝试将第一个键:值对的值应用于第二个键:数组内的每个值,同时从books数组中删除键,从而产生一个列表,该列表接受此输入:< / p>

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)

4 个答案:

答案 0 :(得分: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中的每个项目,我们应用一个函数并将结果收集到一个数组中。现在,该函数实际上将另一个函数应用于其所有书籍并返回一个数组作为结果。第二个功能(适用于所有书籍),返回当前作者,书名和价格。

答案 1 :(得分:2)

地图和地图的组合将为您提供诀窍

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分: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)

答案 3 :(得分: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生成具有所需形状的数组。

相关问题