[英]Unable to add second array object value into first array of objects in javascript
hi i have two arrays i want to merge two arrays, can any one help one please, i have for used for loop pushing that values but its creating duplicates.嗨,我有两个数组,我想合并两个数组,任何人都可以帮忙吗,我有 for 循环推送这些值,但它会创建重复项。
const firstArray = [
{
first: "01",
data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }]
},
{
first: "02",
data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }]
},
{
first: "03",
data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }]
}
];
const secondArray = [{ data: "abc" }, { data: "efg" }, { data: "hij" }];
I need result like this can any one,我需要这样的结果,任何人都可以,
[
{
first: "01",
data: [
{ id: "012345", data: "abc" },
{ id: "0123456", data: "efg" },
{ id: "0123457", data: "hij" },
],
},
{
first: "02",
data: [
{ id: "9998989", data: "abc" },
{ id: "1223", data: "efg" },
{ id: "345666", data: "hij" },
],
},
{
first: "03",
data: [
{ id: "567888", data: "abc" },
{ id: "2345", data: "efg" },
{ id: "09876", data: "hij" },
],
},
];
Rather than using nested for loops to build your array from scratch, you can instead use two .map()
methods.您可以使用两个.map()
方法,而不是使用嵌套的 for 循环从头开始构建数组。 One for mapping your outer objects in firstArray
to new objects with a new data
value, and another for mapping your data
to a merged version from the data in secondArray
.一个用于将firstArray
的外部对象映射到具有新data
值的新对象,另一个用于将您的data
从secondArray
的数据映射到合并版本。 To merge, you can take the corresponding object from secondArray
using the index and then using the spread syntax to merge both objects together:要合并,您可以使用索引从secondArray
获取相应的对象,然后使用扩展语法将两个对象合并在一起:
const firstArray = [ { first: "01", data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }] }, { first: "02", data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }] }, { first: "03", data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }] } ]; const secondArray = [{ data: "abc" }, { data: "efg" }, { data: "hij"}]; const res = firstArray.map(obj => ({ ...obj, data: obj.data.map((inner, i) => ({...inner, ...secondArray[i]})) })); console.log(res);
To fix your current implementation, you can create two arrays, one outside of your for loop like you currently are already doing, and then one inside your first for loop.要修复您当前的实现,您可以创建两个数组,一个在您的 for 循环之外,就像您目前已经在做的那样,然后一个在您的第一个 for 循环中。 The inner array will represent your new data array, which you can then update for your current object:内部数组将代表您的新数据数组,然后您可以为当前对象更新它:
const firstArray = [{ first: "01", data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }] }, { first: "02", data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }] }, { first: "03", data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }] } ]; const secondArray = [{ data: "abc" }, { data: "123" }, { data: "xyz" }]; const emptyArray = []; for (let i = 0; i < firstArray.length; i++) { const data = firstArray[i].data; const dataArray = []; for (let j = 0; j < data.length; j++) { const newObject = { ...data[j], ...secondArray[j] }; dataArray.push(newObject); } emptyArray.push({...firstArray[i], data: dataArray}); } console.log(emptyArray);
Assuming the data
array and second array size are same.假设data
数组和第二个数组大小相同。 Use nested map
method calls to build the aggregation使用嵌套的map
方法调用来构建聚合
const firstArray = [ { first: "01", data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }], }, { first: "02", data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }], }, { first: "03", data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }], }, ]; const secondArray = [{ data: "abc" }, { data: "efg" }, { data: "hij" }]; const output = firstArray.map(({ first, data }) => ({ first, data: data.map((item, i) => ({ ...item, ...secondArray[i] })), })); console.log(output)
You can easily achieve the result using map in js您可以在js中使用map轻松实现结果
const firstArray = [ { first: "01", data: [{ id: "012345" }, { id: "0123456" }, { id: "0123457" }], }, { first: "02", data: [{ id: "9998989" }, { id: "1223" }, { id: "345666" }], }, { first: "03", data: [{ id: "567888" }, { id: "2345" }, { id: "09876" }], }, ]; const secondArray = [{ data: "abc" }, { data: "efg" }, { data: "hij" }]; const result = firstArray.map((obj) => ({ ...obj, data: obj.data.map((o, i) => ({ ...o, ...secondArray[i] })), })); console.log(result);
according to your desired result you should do something like this:根据您想要的结果,您应该执行以下操作:
let emptyArrray = [];
for (let i = 0; i < firstArray.length; i++) {
const dataa = firstArray[i].data;
for (let j = 0; j < dataa.length; j++) {
// console.log(secondArray[i]);
let newObject = {
...secondArray[j],
...dataa[j]
};
emptyArrray.push(newObject);
}
}
in your code you are getting 3*9 long array because you use nestled loop where you shouldn't, you just run over the array once, and add members from array two, also you used在您的代码中,您将获得 3*9 长数组,因为您在不应该使用的嵌套循环中使用了嵌套循环,您只需遍历该数组一次,然后添加数组 2 中的成员,您也使用过
...secondArray[i],
...dataa[i]
instead of代替
...secondArray[j],
...dataa[j]
when incrementing j in the for loop在 for 循环中增加j时
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.