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

156
Views
Repair increment data in array (js)

I am trying to correct the incremented value in the data I received. It looks more or less like this:

const data = [1,2,4,5,6,7];
const correctResult = [1,2,3,4,5,6];

What I would like to get is rightly incremented values + 1 but so as to preserve the possible indexes.

I wrote such a function but it is not efficient:

let array = [1,2,4,5,6,7];
array.map((item, index, array) => {
  if (item === array[index + 1] - 1) {
    return;
  }

  array[index + 1] = array[index + 1] - 1;
});

console.log(array)

The programming code is javascript. I would like to write a good code to be a good model for me.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

not sure if this is the exact answer you seek, but you can try this too

const data = [1, 2, 4, 5, 6, 7];
const correctResult =  data.map((item, index) => {
if (item - 1 !== index) {
  return  --item;
}
else {
  return item;
}
});
console.log(correctResult);

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!