简体   繁体   English

在JavaScript中将几个数组组合成一个对象数组

[英]Combine several arrays into an array of objects in JavaScript

Say I have three arrays depicting some names, number of books read and how awesome these people [in names] are: 假设我有三个数组,描述了一些名称,阅读的书籍数量以及这些人[名字]中有多棒:

let names = ["Mary", "Joe", "Kenan"];
let numberOfBooks = [2, 1, 4];
let awesomenessLevel = ["pretty cool", "meh", "super-reader"];

I'm trying to use .reduce() to bring them together to create an array of objects containing the relevant index in each array, but I am failing miserably: 我正在尝试使用.reduce()将它们组合在一起以创建一个包含每个数组中相关索引的对象数组,但我失败了:

    let people = [
    {
       name: "Mary",
       noOfBooks: 2,
       awesomeness: "pretty cool"
    },
    {
       name: "Joe",
       noOfBooks: 1,
       awesomeness: "meh"
    },
    {
       name: "Kenan",
       noOfBooks: 4,
       awesomeness: "super-reader"
    }
  ]

I got it with reduce as well: 我也减少了它:

let arrFinal = [];

 names.reduce(function(all, item, index) {
  arrFinal.push({
    name: item,
    noOfBooks: numberOfBooks[index],
    awesomeness: awesomenessLevel[index]
  })
}, []);

You could do it with map , like this: 你可以用map来做,像这样:

let result = names.map( (v, i) => ({
    name: names[i], 
    noOfBooks: numberOfBooks[i],
    awesomenessLevel: awesomenessLevel[i]
}));

 let names = ["Mary", "Joe", "Kenan"]; let numberOfBooks = [2, 1, 4]; let awesomenessLevel = ["pretty cool", "meh", "super-reader"]; let result = names.map( (v, i) => ({ name: names[i], noOfBooks: numberOfBooks[i], awesomenessLevel: awesomenessLevel[i] })); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

map works better than reduce in this case, because the number of elements you have in the names array (or any of the two others) is the same as the number of elements you need in the output. 在这种情况下, mapreduce更好,因为names数组中的元素数量(或其他两个元素中的任何一个)与输出中所需的元素数量相同。 In that case it is more natural to use map . 在这种情况下,使用map更自然。

Use map to create a 1-to-1 mapping between the input arrays and the output arrays. 使用map在输入数组和输出数组之间创建1对1映射。

let people = names.map(function (e, i) {
    return {name:e, noOfBooks:numberOfBooks[i],awesomeness: awesomenessLevel[i]};
});

 let names = ["Mary", "Joe", "Kenan"]; let numberOfBooks = [2, 1, 4]; let awesomenessLevel = ["pretty cool", "meh", "super-reader"]; let people = names.map(function (e, i) { return {name:e, noOfBooks:numberOfBooks[i],awesomeness: awesomenessLevel[i]}; }); console.log(people); 

You could use a dynamic approach by combining all arrays to one object and use the key names as property names for the result objects in the array 您可以通过将所有数组组合到一个对象并使用键名作为数组中结果对象的属性名来使用动态方法

 let names = ["Mary", "Joe", "Kenan"], numberOfBooks = [2, 1, 4], awesomenessLevel = ["pretty cool", "meh", "super-reader"], object = { name: names, noOfBooks: numberOfBooks, awesomeness: awesomenessLevel }, result = Object.keys(object).reduce((r, k) => (object[k].forEach((a, i) => (r[i] = r[i] || {})[k] = a), r), []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM