Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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.

  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" },
    ],
  },
];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!