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

82
Visualizações
pushing multiple items into an object array

forgive me, as I am still new so I hope I am explaining this so it makes sense

I wanted to push multiple items into the addItem function (for instance) console.log("forks", "laptop", "juice") I am able to only push in first item. If I split the strings into separate arguments, when I call back the function, I only get the last argument.

  const cart = {
  contents: [],
  addItem(item) {
    cart.contents = (item) 
    contents.push(...item); //tried it with and without the spread operator.
},
  removeItem(deletedItem) {
    delete cart.contents[deletedItem];
  }
};
cart.addItem("laptop");
cart.addItem("guitar");
cart.addItem("phone");
cart.removeItem("guitar")
console.log(`The cart contains: ${cart.contents}`);

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

0

You have to make item a rest parameter:

const cart = {
  contents: [],
  addItem(...item) {
    this.contents.push(...item);
  },
  removeItem(deletedItem) {
    this.contents.splice(this.contents.indexOf(deletedItem), 1);
  }
};
cart.addItem("laptop", "guitar", "phone");
cart.removeItem("guitar")
console.log(`The cart contains: ${cart.contents}`);

Also, don't use delete to delete an item. Instead, use splice().

about 4 years ago · Juan Pablo Isaza Relatório

0

you were pretty close with the spread operator, your code just needed a little change, with this change you can pass multiple items as separated values or even an array with multiple items, even multiple arrays with multiple values.

they end always being a plain array of strings, here is your working code.

const cart = {
  contents: [],
  addItem: (...items) => {
    cart.contents = [...cart.contents, ...items];
  },
  removeItem(deletedItem) {
    this.contents.splice(this.contents.indexOf(deletedItem), 1);
  }
};

// multiple values
cart.addItem("laptop", "guitar", "phone");
console.log(`The cart contains: ${cart.contents}`);

// array of values
cart.addItem(['new laptop', 'new guitar', 'new phone']);
console.log(`The cart contains: ${cart.contents}`);

// multiple arrays
cart.addItem(['inner laptop1', 'inner guitar1', 'inner phone1'], ['inner laptop2', 'inner guitar2', 'inner phone2'], ['inner laptop3', 'inner guitar3', 'inner phone3']);
console.log(`The cart contains: ${cart.contents}`);

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