简体   繁体   English

如何将 2 个数组连接到一个对象数组中

[英]How to join 2 arrays into an array of objects

I've got some headers and peopleData :我有一些headerspeopleData

const headers = ["name", "age", "nationality"]

const peopleData = [
  ["John", 31, "Spanish"],
  ["Jane", 41, "Italian"],
  ["Johnson", 11, "Thai"],
  ["Rob", 13, "Japanese"],
]

I want to combine both and return an array of objects looking like:我想将两者结合起来并返回一个对象数组,如下所示:

[{
  name: "John",
  age: 31,
  nationality: "Spanish"
}, {
  name: "Jane",
  age: 41,
  nationality: "Italian"
}, {
  name: "Johnson",
  age: 11,
  nationalityL: "Thai"
}, {
  name: "Rob",
  age: 13,
  nationality: "Japanese"
}]

So far I came up to this:到目前为止,我想出了这个:

const people = peopleData.map((person, i) => {
  return headers.map((header, index) => {
    return {
      [header]: person[index]
    }
  })
})

This solution does not work since it creates nested objects:此解决方案不起作用,因为它创建了嵌套对象:

[[{
  name: "John"
}, {
  age: 31
}, {
  nationality: "Spanish"
}], [{
  name: "Jane"
}, {
  age: 41
}, {
  nationality: "Italian"
}], [{
  name: "Johnson"
}, {
  age: 11
}, {
  nationality: "Thai"
}], [{
  name: "Rob"
}, {
  age: 13
}, {
  nationality: "Japanese"
}]]

Example below:下面的例子:

Credit to Andreas comment, better performant below归功于 Andreas 评论,下面的性能更好

 const headers = ["name", "age", "nationality"]; const peopleData = [ ["John", 31, "Spanish"], ["Jane", 41, "Italian"], ["Johnson", 11, "Thai"], ["Rob", 13, "Japanese"], ]; const o = peopleData.map(a => a.reduce((acc, b, i) => { acc[headers[i]] = b; return acc; }, {}) ); console.log(o);

In one line-er, but less performent在一行中,但性能较差

 const headers = ["name", "age", "nationality"]; const peopleData = [ ["John", 31, "Spanish"], ["Jane", 41, "Italian"], ["Johnson", 11, "Thai"], ["Rob", 13, "Japanese"], ]; const o = peopleData.map(a => a.reduce((acc, b, i) => ({ ...acc, [headers[i]]: b }), {}) ); console.log(o);

You can wrap your inner .map() in a call to Object.fromEntries() which will build an object for you - it takes an array of [[key, value],...] pairs, and so you can change your inner map to return a [key, value] pair array instead of objects like so:您可以将内部.map()包装在对Object.fromEntries()的调用中,它将为您构建一个对象 - 它需要一个[[key, value],...]对数组,因此您可以更改您的内部映射返回一个[key, value]对数组而不是像这样的对象:

 const headers = ["name", "age", "nationality"]; const peopleData = [ ["John", 31, "Spanish"], ["Jane", 41, "Italian"], ["Johnson", 11, "Thai"], ["Rob", 13, "Japanese"], ]; const res = peopleData.map(person => Object.fromEntries(person.map( (val, i) => [headers[i], val] ))); console.log(res);

Or, you can stick with your approach of returning an array of objects, but then merge the objects within the array together using Object.assign() and the spread syntax ... :或者,您可以坚持使用返回对象数组的方法,然后使用Object.assign()扩展语法将数组中的对象合并在一起...

 const headers = ["name", "age", "nationality"]; const peopleData = [ ["John", 31, "Spanish"], ["Jane", 41, "Italian"], ["Johnson", 11, "Thai"], ["Rob", 13, "Japanese"], ]; const res = peopleData.map(person => Object.assign(...person.map( (val, i) => ({[headers[i]]: val}) ))); console.log(res);

A simply solution easy to understand!一个简单易懂的解决方案! Iterate all peopleData and put them in a new object using the array headers :迭代所有peopleData并使用数组headers将它们放入一个新对象中:

 const headers = ["name", "age", "nationality"]; const peopleData = [["John", 31, "Spanish"],["Jane", 41, "Italian"],["Johnson", 11, "Thai"],["Rob", 13, "Japanese"]]; let result = []; peopleData.forEach((e, i) => { //iterate data result[i] = {}; result[i][headers[0]] = e[0]; result[i][headers[1]] = e[1]; result[i][headers[2]] = e[2]; }); console.log(result);

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

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