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

88
Visualizações
Adding new array to object without deleting the previous array

I have an array obj, and I am and Im adding another object links and I am creating a serie array in the object. I want to create another movie array after the serie array.

const obj = {
projectId: 0,
gridId: 0,
createValues: [
{
  field: "string",
  value: "string"
}
]
};

const dataCompo = {
"model-10389": 164703,
"model-10388": 164704,
"model-10387": 164705
};

const dataTraca = {
"model-10389": [1656, 1234, 1245],
"model-10384": [1656, 1234, 1245],
"model-10383": [1656, 1234, 1245],

};

const ser = Object.entries(dataCompo).map(([key, value]) => ({
modelId: key.substring(6),
id: value
}));
obj.links = {
        serie: ser
    };

const mov = Object.entries(dataTraca).map(([key,value]) => ({
modelId: key.substring(6),
ids: value
}));

obj.links = {
        movie: mov
    };

console.log(obj);

The thing is like this it is remplacing the whole links object and serie array with the movie one. I cannot quite figure how to use the spread operator.

I have managed to do it in one line like this :

    obj.links = { serie: ser, movie:mov };

However I would like to do it seperately just like above

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

0

you can do something like this

const obj = {
  projectId: 0,
  gridId: 0,
  createValues: [{
    field: "string",
    value: "string"
  }]
};

const dataCompo = {
  "model-10389": 164703,
  "model-10388": 164704,
  "model-10387": 164705
};

const dataTraca = {
  "model-10389": [1656, 1234, 1245],
  "model-10384": [1656, 1234, 1245],
  "model-10383": [1656, 1234, 1245],

};

const newData = [
    ['movie', dataCompo],
    ['serie', dataTraca]
  ].reduce((res, [key, value]) => {
      return {
        ...res,
        [key]: Object.entries(value).map(([key, value]) => ({
          modelId: key.substring(6),
          id: value
        }))
    }
  },
  obj)

console.log(newData)

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