扁平化数组中的对象

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

Where I get stuck

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个回答

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

你需要一种类似于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 => {
   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来生成您想要的形状的数组。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接