I have a requirement to find specific record from array and move that to first position (0 index) and 0 index should move to next index and so on.
I do not want to remove or duplicate any record from array. This is what I have tried, it gives error splice is not defined
var record = data.find(x => x.CodeLovId == existingGridCellItem);
if (record !== null) {
data.remove(record)
data.splice(0, 0, record);
}
And below code duplicates records and removes existing
var record = data.find(x => x.CodeLovId == existingGridCellItem);
if (record !== null) {
data[0] = record
}
Ex –
arr1 = ['YES', 'NO', 'OK']
Item to move ['NO']
Ans - arr1 = ['NO', 'YES', 'OK']
How can I do it ?
>Solution :
- get the index of the wanted item to be moved to top.
- check if index is in array and
- unshift spliced item
const
array = [1, 2, 3, 4, 5],
index = array.findIndex(v => v === 4);
if (index !== -1) array.unshift(...array.splice(index, 1));
console.log(array);