Returns an array of objects where a value of a specific key can only occur once. The first instance of the key/value pair is preserved and subsequent instances are removed. You can optionally pass in a boolean to make detection case sensitive.
function removeDuplicatesByKeyValue<T extends object, U extends keyof T>(
arr: T[],
key: U,
isCaseSensitive = false
)
// Example:
const members = [
{ id: 1, name: "Stephen" },
{ id: 2, name: "Andrea" },
{ id: 1, name: "Monica" },
{ id: 4, name: "Dylan" },
]
removeDuplicatesByKeyValue(members, "id")
//=>
[
{ id: 1, name: "Stephen" },
{ id: 2, name: "Andrea" },
{ id: 4, name: "Dylan" },
]