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

130
Visualizações
How to add data to an object in an array

So I want to add some data to an array.

this is my array
var x = [
{some data},
{some data},
{some data} 
]
this is my new data I want to add to an object in the array x

var newData = [
{}
]

I want this as result:

x = [
{some data},
{some data, {newData} },
{some data} 
]

I know about the push function but this just adds the newData to the array. I want to add it to an object inside the array.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your array needs to have some kind of identifier for each object (Or you can use the index of the object inside the array, which might change, depending on your actions).

const dataArray = [
  {id: 1, /* .. other data */},
  {id: 2, /* .. other data */},
  {id: 3, /* .. other data */}
]

The following method receives an identifier to search for the object inside the array. If it was found, the new data will be added to this very object.

function addToX(identifier: number, newData: object) {
  const obj = dataArray.find((x) => x.id === identifier);
  
  if (obj) {
    obj.newData = {...newData};
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not an expert, but I think you could try this: x=x.concat(newData)

about 4 years ago · Juan Pablo Isaza Relatório

0

let x:any =[];
let y:any =[];

 x= [
    {'data' : 1},
    {'data' : 2},
    {'data' : 3},
];

y =[
    {'newData' : 1},
    {'newData' : 2},
    {'newData' : 3},
];

x.push({'data':y});

console.log(x);
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