function swapArrayElements(arr: any, key: number) {
const index = getKeyIndex(arr, key);
console.log(index)
if (index === -1 ) {
throw new Error('One or both users not found');
}
if (index !== -1 && (index-1) !== -1) {
[arr[index], arr[index-1]] = [arr[index-1], arr[index]];
}
return arr
}
function getKeyIndex(arr: any, key: any) {
return arr.findIndex((item: { key: any }) => item.key === key);
}
const originalArray= [
{ key: 1, value: 'A' },
{ key: 2, value: 'B' },
{ key: 3, value: 'C' },
{ key: 5, value: 'D' },
{ key: 4, value: 'E' },
];
let res = swapArrayElements(originalArray,5)
console.log(res)