How to update properties using javascript and get results with property values ​​in consecutive order in array object
P粉980815259
P粉980815259 2024-04-04 13:32:46
0
1
555

I have array of objects list, parameters rid and rorder are passed.

-> 1. If rid is the same as id in the list, update the itemorder > value with rorder.

-> 2.UpdatedList uList Attribute itemorder If the same value exists, increment the value until its consecutive order (1,2, 3...n)

var rid = 23; var rvalue = 2
const list = [
  { id: 12, itemorder:null, place : 'IN' },
  { id: 24, itemorder: 1, place: 'MY' }, 
  { id: 23, itemorder: 4, place: 'AU' },
  { id: 44, itemorder: 3, place: 'SG' },
  { id: 54, itemorder: 2, place: 'CN' }, 
];

//updated List will be
const uList = [
  { id: 12, itemorder:null, place : 'IN' },
  { id: 24, itemorder: 1, place: 'MY' }, 
  { id: 23, itemorder: 2, place: 'AU' }, // updates since id and rid same // 2
  { id: 44, itemorder: 3, place: 'SG' }, // 4
  { id: 54, itemorder: 2, place: 'CN' }, // 3
];

// uList property itemorder has same value here `2`, increment until consecutive (1,2, 3...n).

Expected Output

[
  { id: 12, itemorder:null, place : 'IN' },
  { id: 24, itemorder: 1, place: 'MY' }, 
  { id: 23, itemorder: 2, place: 'AU' },
  { id: 44, itemorder: 4, place: 'SG' },
  { id: 54, itemorder: 3, place: 'CN' }, 
];
Tried 

var listChanged = list.map(e=> ({
  ...e,
  itemorder: e.id===rid ? rvalue : e.itemorder
}))

var result = listChanged.map(i => ({
  ...e,
  itemorder: (i.itemorder !== rvalue && i.itemorder > rvalue) ? (i.itemorder || 0)+1 : i.itemorder
}));
when passing var rid = 23; var rvalue = 1

Expected Output:

[
  { id: 12, itemorder:null, place : 'IN' },
  { id: 24, itemorder: 2, place: 'MY' }, // 1 becomes 2
  { id: 23, itemorder: 1, place: 'AU' }, // updates since both id and rid same
  { id: 44, itemorder: 4, place: 'SG' },//3 becomes 4
  { id: 54, itemorder: 3, place: 'CN' }, //2 becomes 3
]

no repeat values so increment, follows consecutive order (1,2,3..n)

P粉980815259
P粉980815259

reply all(1)
P粉865900994

var rid = 23; var rvalue = 2
const list = [
  { id: 12, itemorder: null, place: 'IN' },
  { id: 24, itemorder: 1, place: 'MY' },
  { id: 23, itemorder: 4, place: 'AU' },
  { id: 44, itemorder: 3, place: 'SG' },
  { id: 54, itemorder: 2, place: 'CN' },
];

let ridIdx = list.findIndex(x => x.id === rid)

orderedList = list.map((l,i) => {
    if(i === ridIdx) {
        return {
        ...l,
        itemorder: rvalue
        }
    }
    if(i > ridIdx) {
        return {
            ...l,
            itemorder: l.itemorder + 1
        }
    }
    return l
})

console.log(orderedList)

The above code should work. Based on what I understand from your question so far, we do the following steps:

  1. Find an item with a given rid in a list
  2. Now map the list and based on the index found in step 1, return by default, if the item index matches the index above, return the itemorder as an rvalue and by incrementing the existing itemorder Returns If the item index is greater than the found index, the value is increased by 1
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template