[Refactor] Prefer for loop over reduce (#764)

- Follow-up on #763
This commit is contained in:
filtered
2025-03-13 10:12:04 +11:00
committed by GitHub
parent 77465113cd
commit cca2ac28e0

View File

@@ -12,11 +12,12 @@ export function omitBy<T extends object>(obj: T, predicate: (value: any) => bool
* @returns A new object with just the picked properties
*/
export function pick<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
// eslint-disable-next-line unicorn/no-array-reduce
return keys.reduce((result, key) => {
// Assertion: Required to avoid verbose runtime guards
const picked = {} as Pick<T, K>
for (const key of keys) {
if (key in obj) {
result[key] = obj[key]
picked[key] = obj[key]
}
return result
}, {} as Pick<T, K>)
}
return picked
}