展平数组内的对象

Posted

技术标签:

【中文标题】展平数组内的对象【英文标题】:flatten object inside array 【发布时间】:2017-05-13 01:48:16 【问题描述】:

我正在尝试将第一个 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)

【问题讨论】:

【参考方案1】:

你可以像这样映射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【参考方案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);

【讨论】:

【参考方案3】:

我只是循环:

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)

【讨论】:

【参考方案4】:

这个怎么样:

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 来生成你想要的形状的数组。

【讨论】:

以上是关于展平数组内的对象的主要内容,如果未能解决你的问题,请参考以下文章

具有嵌套对象的多个对象的展平数组

展平对象中的数组

将包含对象的数组展平为 1 个对象

展平 Powershell 对象中的子数组,包括父对象属性

雪花中具有多个 JSON 对象的横向展平数组

PHP展平JSON对象数组?