I have an array that I want to find an item with an ID of 1.
Then change the index of the found item from 0 to 1.
This is not working properly. Please guide me to get the answer.
My expected output:
[
{id:2,name:"sara"},
{id:1,name:"jhon"},
{id:3,name:"James"},
]
const arr=[
{id:1,name:"jhon"},
{id:2,name:"sara"},
{id:3,name:"James"},
]
function move(arry, from, to) {
let numberOfDeletedElm = 1;
const elm = input.splice(from, numberOfDeletedElm)[0];
numberOfDeletedElm = 0;
arry.splice(to, numberOfDeletedElm, elm);
}
const findIndex =arr.findIndex((element) => element.id === 1);
const result = move(arr, findIndex, findIndex++);
console.log(result);