Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda