Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

321
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!