简体   繁体   English

将项目从另一个数组添加到 object 数组

[英]Adding items to an object array from another array

I have an array like this:我有一个这样的数组:

data = [1, 2, 3, 4, 5]

and I have an array of objects like this:我有一个这样的对象数组:

obj = [{"Name":"ABC","Age":25,"Gender":"M"},
       {"Name":"DEF","Age":32,"Gender":"F"},
       {"Name":"PQR","Age":30,"Gender":"F"},
       {"Name":"XYZ","Age":30,"Gender":"F"}]

I need to push each element of the data array into each object of the array.我需要将数据数组的每个元素推入数组的每个 object 中。 My expected output is this:我预期的 output 是这样的:

obj = [{"Name":"ABC","Age":25,"Gender":"M", "Data":1}, 
       {"Name":"DEF","Age":32,"Gender":"F", "Data":2}, 
       {"Name":"PQR","Age":30,"Gender":"F", "Data":3}, 
       {"Name":"XYZ","Age":30,"Gender":"F", "Data":4}]

I tried like this:我试过这样:

for(let i = 0; i<data.length; i++){
   obj.push({data:data[i])
}

But that gave an incorrect result like this:但这给出了这样的错误结果:

obj = [{"Name":"ABC","Age":25,"Gender":"M"},
       {"Name":"DEF","Age":32,"Gender":"F"}, 
       {"Name":"PQR","Age":30,"Gender":"F"},
       {"Name":"XYZ","Age":30,"Gender":"F"}, 
       {"Data":1},{"Data":2},{"Data":3},{"Data":4}]

I understand that this is because I am not iterating through array of object before pushing the items into it.我知道这是因为在将项目推入之前我没有遍历 object 数组。 But I am unable to iterate through data as well as obj together.但是我无法同时遍历数据和 obj。 Please help me solve the issue.请帮我解决问题。 Thanks in advance.提前致谢。

There are multiple ways in achieving the expected output.有多种方法可以实现预期的 output。 Here is one of the ways, Loop through the array using Array.map and destructuring这是其中一种方法,使用Array.map遍历数组并解构

 const data = [{"Name":"ABC","Age":25,"Gender":"M"}, {"Name":"DEF","Age":32,"Gender":"F"},{"Name":"PQR","Age":30,"Gender":"F"},{"Name":"XYZ","Age":30,"Gender":"F"}]; const array = [1, 2, 3, 4, 5]; const formattedData = (data, array) => data.map((obj, i) => ( {...obj, Data: array[i] } )); console.log(formattedData(data, array));
 .as-console-wrapper { max-height: 100%;important; }

It can be solved by a spread operator.它可以通过扩展运算符来解决。 Please have a look as following codes.请看下面的代码。

 obj = [{"Name":"ABC","Age":25,"Gender":"M"}, {"Name":"DEF","Age":32,"Gender":"F"}, {"Name":"PQR","Age":30,"Gender":"F"}, {"Name":"XYZ","Age":30,"Gender":"F"}] data = [1,2,3,4,5] newObj = obj.map((item, i)=>({...item, Data: data[i]})); console.log(newObj);

Given that the length of the 2 arrays are the same:鉴于 2 arrays 的长度相同:

for (let i = 0; i < data.length; i++) {
    obj[i].Data = data[i];
}
let result = obj.map((e,i) => ({...e, "Data":data[i]}));

console.log(result);

const data = [{"Name":"ABC","Age":25,"Gender":"M"}, {"Name":"DEF","Age":32,"Gender":"F"},{"Name":"PQR","Age":30,"Gender":"F"},{"Name":"XYZ","Age":30,"Gender":"F"}];常量数据 = [{"Name":"ABC","Age":25,"Gender":"M"}, {"Name":"DEF","Age":32,"Gender":"F" },{"姓名":"PQR","年龄":30,"性别":"F"},{"姓名":"XYZ","年龄":30,"性别":"F"}] ; const array = [1, 2, 3, 4, 5];常量数组 = [1, 2, 3, 4, 5];

Few things to consider here is we need new object where the length remains same as data and array.这里要考虑的几件事是我们需要新的 object,其中长度与数据和数组相同。 With the help of map function, we can get eachData and index of the eachData and we can mutate the object by adding Data in this case.在 map function 的帮助下,我们可以得到 eachData 和 eachData 的索引,我们可以在这种情况下通过添加Data来改变 object。

We need to add closing bracket as Rest parameter must be last formal parameter forgetting which would throw syntax error for the same我们需要添加右括号,因为Rest 参数必须是最后一个形式参数忘记,这会引发语法错误

const formattedData = (data,array) => data.map((eachData,i)=> ({...eachData,Data:array[i]})) const formattedData = (data,array) => data.map((eachData,i)=> ({...eachData,Data:array[i]}))

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

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