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

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

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 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!