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

155
Visualizações
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
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