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

152
Views
Javascript getter / setter with an array: how can I automatically set a property based on the array item index?

Let’s say I have a playlist class like this one:

class playlist{
  constructor(urls){
      this.tracks=urls;
  }
}

I want to use a javascript setter to create an array of track objects based on the urls input.

class track{
  constructor(url,num){
      this.url=url;
      this.num = num;
  }
}

class playlist{

  constructor(urls){
      this.tracks=urls;
  }

  //tracks setter
  set tracks(urls){
      this.tracks = urls.map(function(url,index) {
          return new track(url,index+1);
      })
  }
}

This is fine.
But if I remove some items of the tracks array of the playlist object, if I slice it, etc; the num properties of the track objects will not « update » : I want their num property to be always the track index+1.

Is it possible to automate this and how?

EDIT The idea behind those classes is to extend standardized JSPF objects, in the goal of having {...playlistdata} or {...trackdata} output a regular JSPF object - the code above has been simplified for the question.

Thanks!

about 4 years ago · Juan Pablo Isaza
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!