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

211
Visualizações
Javascript replace value to increment number

i'm trying to achieve something using Javascript, my current condition is ,i have array that has same value, i need to replace the value to increment number if the value are same.

Here is my sample code

var period = ['202001', '202001', '202001', '202002', '202002', '202003', '202003', ...];

The result i'm expecting is

var period = ['0', '0', '0', '1', '1', '2', '2'];

The real case is, i want to produce a heatmap using highcharts, but i need to convert the year-month into xy coordinates.

Do you have any best suggestion about how do i solve this ?
Many Thanks...

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

0

Have a look below;

var period = ['202001', '202001', '202001', '202002', '202002', '202003', '202003', ];

let prevString = period[0];
let lastNumber = 0;
let reqArr = period.map(string => {
    if (string == prevString) {
        return lastNumber.toString();
    } else {
        prevString = string;
        lastNumber+=1
        return lastNumber.toString();
    }
})

console.log(reqArr);

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