[英]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.entries
, reduce
和forEach
快速实现:
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: 说明:
Object.entries
to get turn the object into an array of key-value pairs. Object.entries
可以将对象转换为键值对数组。 .reduce
to iteratively build up the result array. .reduce
迭代地构建结果数组。 .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.