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

324
Visualizações
How can I increment content of object and it's properties and properties value?
var groups = new Truck([
    {id: num, content: `Truck ${num}`}
]);

In a constructor, I am trying to increment the whole object inside an array on a button click. I also want to increase the Number(num) of the id and content. And, I want to use a button for this whole process, when I click a button an array should add one more object inside itself along with the number incremented, like this:

var groups = new Truck([
    {id: 1, content: `Truck 1`},
    {id: 2, content: `Truck 2`},
    {id: 3, content: `Truck 3`},
    {id: 4, content: `Truck 4`}
]);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. Use the class constructor to declare an id and an array.

  2. Declare an add method that creates a new object based on the current id, pushes it in to the array, and then increments the id.

  3. Create an instance of Truck.

  4. Create a button, and attach a listener to it that calls a function that calls add on the instance, and then logs it.

class Truck {

  constructor() {
    this.id = 1;
    this.arr = [];
  }
  
  add() {
    this.arr.push({
      id: this.id,
      content: `Truck ${this.id}`
    });
    ++this.id;
  }

}

const group = new Truck();

const button = document.querySelector('button');
button.addEventListener('click', handleClick, false);

function handleClick() {
  group.add();
  console.log(group);
}
<button>Add new object</button>

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