I'm trying to eliminate the similar array of objects with the referent of name key in object with this function getUniqueListBy but it's giving the output by deleting the first duplicated elements in the array
I want to delete the duplicated from last by reference with the name
Sample input:
[
{ name:'shanu', id:1 },
{ name:'bhanu', id:2 },
{ name:'chary', id:3 },
{ name:'shanu', id:4 },
{ name:'rahul', id:5 }
]
Expected Output:
[
{ name:'shanu', id:1 },
{ name:'bhanu', id:2 },
{ name:'chary', id:3 },
{ name:'rahul', id:5
]
What I did:
function getUniqueListBy(data, key) {
return [
...new Map(data.map((item) => [item[key], item])).values(),
];
}
const data = [ { name:'shanu', id:1 }, { name:'bhanu', id:2 }, { name:'chary', id:3 }, { name:'shanu', id:4 }, { name:'rahul', id:5 } ];
const arr1 = getUniqueListBy(data, "name");
console.log(arr1);
Current Output:
[
{ name:'bhanu', id:2 },
{ name:'chary', id:3 },
{ name:'shanu', id:4 },
{ name:'rahul', id:5 }
]
it's deleted the first item but I want to delete similar data from last.