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

123
Visualizações
JS: Add objects form an array to objects inside another array

DISCLAIMER: Please dont mind my stupid object names, its just to learn the basics

I have an array of objects named "cars":

const cars = [
  {
    name: "red",
    competes: true,
    category: 2,
    given: 400
  },
  {
   name: "blue",
   competes: false,
   category: 2,
    given: 0
  },
  {
    name: "green",
    competes: true,
     category: 2,
    given: 0
  }
]

And another array of objects named addOns:

const addOns = [
  {
    name: "hyperblast",
    upgrade: 100
  },
  {
    name: "catalyst",
    upgrade: 400
  }
]

Question

How can i add for example the addOn {name: "catalyst", upgrade: 400} to the car named "green"?
Ive tried with

cars[2].push(addOns[1]);

but it doesnt work. And all my google searches were without result (arrays of objects seem so important but I constantly fail to find anything regarding. Only about arrays or objects, never together)

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

0

You could add the addOns as another field of the car object. For your example: cars[2].extra = addOns[0]; . After typing this, if you try to console the cars[2] you will see:

{
  name: 'green',
  competes: true,
  category: 2,
  given: 0,
  extra: { name: 'hyperblast', upgrade: 100 }
}

Since you are working with vanilla Js, the cars[0].extra is going to work, but if you use TS in future, it would be better to define a field before adding it, or changing the structure of the script completely

about 4 years ago · Juan Pablo Isaza Relatório

0

As cars and addOns both have the property key 'name', I would change the second one to 'addOnName' or something of that sort. Otherwise, 'green' will just be overwritten to 'hyperblast' in the result.

Being that you're trying to add multiple properties to an object, you should use a for...in loop. As you can see below, I've cycled through the cars array with a regular for loop. If the given car's name is 'green' I call the addProps function and pass it the given car object as an argument. Each prop in your chosen addOns object is then added on to the given car obj.

const addProps = (obj) => {
  for (let prop in addOns[0]) {
    obj[prop] = addOns[0][prop];
  }
}

for (let i = 0; i < cars.length; i++) {
  if (cars[i].name === 'green') {
    addProps(cars[i]);
  }
}

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