简体   繁体   English

如何将一个对象内部的值数组分组到另一个对象数组

[英]how can I group array of values inside of an object to another array of object

I have an object with array of values 我有一个带有值数组的对象


{firstName: ['John', 'Mike']}, lastName: ['Doe', 'Smith']} , {firstName: ['John', 'Mike']}, lastName: ['Doe', 'Smith']}


Now i have an empty object with array 现在我有一个空对象与数组


const newData = {} newData.ppl = [] const newData = {} newData.ppl = []


how can I have this result in ppl array like: ppl = [{firstName: 'John', lastName: 'Doe'}, {firstName: 'Mike', lastName: 'Smith'}] 我如何在ppl数组中得到这样的结果: ppl = [{firstName: 'John', lastName: 'Doe'}, {firstName: 'Mike', lastName: 'Smith'}]

Iterate the firstName with Array.forEach() , and take the first name. 使用Array.forEach()迭代firstName并获取名字。 Use the index to take the respective last name from the lastName array. 使用索引从lastName数组中获取相应的姓氏。 Push a new object with the first and last names to the ppl array. 将具有名字和姓氏的新对象推送到ppl数组。

 const data = {firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']}; const newData = { ppl: [] }; data.firstName.forEach((firstName, i) => newData.ppl.push({ firstName, lastName: data.lastName[i] }) ); console.log(newData); 

A quick implementation with Object.entries , reduce , and forEach : 使用Object.entriesreduceforEach快速实现:

 const srcData = { firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] }; const ppl = Object .entries(srcData) .reduce((acc, [key, values]) => { values.forEach((val, i) => { if (i >= acc.length) { acc.push({}); } acc[i][key] = val; }); return acc; }, []); console.log(ppl); 

Explanation: 说明:

  1. Use Object.entries to get turn the object into an array of key-value pairs. 使用Object.entries可以将对象转换为键值对数组。
  2. Use .reduce to iteratively build up the result array. 使用.reduce迭代地构建结果数组。
  3. Use .forEach to loop over the array of values obtained from (1), pushing an empty object to the array ( push({}) ) if the result array isn't long enough to hold all the items in the current array of values 使用.forEach遍历从(1)获得的值数组,如果结果数组的长度不足以容纳当前值数组中的所有项目,则将一个空对象push({})该数组( push({})

You could get the entries and assign the properties to the same index as the array's values. 您可以获取条目并将属性分配给与数组值相同的索引。

This works for any arbitrary count of properties. 这适用于任意数量的属性。

 var data = { firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] }, result = Object .entries(data) .reduce((r, [k, a]) => (a.forEach((v, i) => (r[i] = r[i] || {})[k] = v), r), []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

using a map. 使用地图。

 let master = {firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']} const newData = {}; newData.ppl = master.firstName.map((f,i) => { return {firstName: f, lastName: master.lastName[i]}; }); console.log(newData.ppl); 

You can do it with Array.reduce() , Object.values() and Object.entries() like this: 您可以使用Array.reduce()Object.values()Object.entries() Array.reduce()做到这一点,如下所示:

 const data = { firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith'] }; const ppl = Object.entries(data).reduce((acc, [key, arr]) => { arr.forEach((v, i) => (acc[i] = (acc[i] || {}))[key] = v); return acc; }, {}); const newData = { ppl: Object.values(ppl) }; console.log(newData); 

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

相关问题 如何按键对对象数组进行分组并在其中创建另一个 object - How can I group an array of objects by key and create another object inside 如何将对象中的值分组并将它们推送到 JS 中的数组中? - How can I group the values in the object and push them in an array in JS? 如何在数组中对 object 进行分组 - How to group object inside array 在对象数组中,如果对象值共享另一个键,如何将它们分组? - Inside an array of objects, how do you group object values if they share another key together? 我可以将一个对象和一个数组放到另一个数组中吗? - Can I put an object and an array together inside another array? 使用嵌套的 object 对数组内的值进行分组和计数 - Group and count values inside array with nested object 如何通过JavaScript中的另一个对象将对象数组分组 - How to group an array of objects by another object inside in javascript 如何将具有属性的 object 数组与另一个数组分组 - How to group an array of object with property with another array 如何从数组内的 object 内的数组中添加 2 个值并将其显示在外的字段中? - How can I add 2 values from array inside object inside array and show it in field outside? 我如何将 .map() 和 Array 嵌套在一个对象内,在一个数组内? - How Can I .map() and Array that is nested inside an object, inside an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM