[SOLVED] How to rearrange number or reset the number

Issue

What I’m trying to do here is when item/data isRemove it will adjust the number.

It will adjust the number in noFrom and noTo.

the current sample code just minus 1.

How to make the data like this

  [
          {noFrom: 1,noTo: 1},
          {noFrom: 2,noTo: 2},
          {noFrom: 3,noTo: 3},
          {noFrom: 4,noTo: 7},
          {noFrom: 8,noTo: 8},
          {noFrom: 9,noTo: 9},
        ]

Solution

how about that


const items = [
      {noFrom: 1,noTo: 1,},
      {noFrom: 2,noTo: 2,isRemove: true},
      {noFrom: 3,noTo: 3,},
      {noFrom: 4,noTo: 4,},
      {noFrom: 5,noTo: 5,isRemove: true,},
      {noFrom: 6,noTo: 9,},
      {noFrom: 10,noTo: 10,},
      {noFrom: 11,noTo: 11,},
    ];

let lt = [...items];
let removeCount = 0;

for(let i of lt) {
    i.noFrom -= removeCount;
    i.noTo -= removeCount;
  if(i.isRemove) {
    removeCount++;
  }
}

lt = lt.filter(i => !i.isRemove);

console.log(lt);

produces this code:

  [
          {noFrom: 1,noTo: 1},
          {noFrom: 2,noTo: 2},
          {noFrom: 3,noTo: 3},
          {noFrom: 4,noTo: 7},
          {noFrom: 8,noTo: 8},
          {noFrom: 9,noTo: 9},
        ]

Answered By – 0brine

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *