Last active
August 2, 2023 16:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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