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.
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. 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. To merge, you can take the corresponding object from secondArray using the index and then using the spread syntax to merge both objects together:
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. 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. Use nested map method calls to build the aggregation
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
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);