Skip to content

Instantly share code, notes, and snippets.

@schuchard
schuchard / recursiveFilterProp.js
Last active June 20, 2024 18:42
Recursively filter out objects that are infinitely nested and return an immutable object
// https://stackblitz.com/edit/recursive-filter-prop?file=index.ts
// perf: https://jsbench.me/8cjrlaine7/1
function flatFilter(nestedProp, compareKey, compareId, arr) {
return arr.filter(o => {
const keep = o[compareKey] != compareId;
if (keep && o[nestedProp]) {
o[nestedProp] = flatFilter(nestedProp, compareKey, compareId, o[nestedProp]);
}
return keep;