Skip to content

Instantly share code, notes, and snippets.

@maiconrs95
Last active August 2, 2023 16:59
Show Gist options
  • Save maiconrs95/e6ad0420ff9be1710e240a2fa0a9ac32 to your computer and use it in GitHub Desktop.
Save maiconrs95/e6ad0420ff9be1710e240a2fa0a9ac32 to your computer and use it in GitHub Desktop.
Used to update current list with next list values using some list key
/**
* Update "current" list with "next" list values using some valid list key
*
* @param current "current" list
* @param next "next" list containing values to update into "current" list
* @param key used to map values to be updated
* @returns updated list
*/
export function updateByListKey<T>(current: T[], next: T[], key: keyof T): T[] {
const listMap = new Map();
current.forEach(object => {
listMap.set(object[key], object);
});
next.forEach(object => {
if (listMap.has(object[key])) {
listMap.set(object[key], Object.assign(listMap.get(object[key]), object));
}
});
return Array.from(listMap, ([, value]) => value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment