Skip to content

Instantly share code, notes, and snippets.

@MikeyBurkman
Last active December 7, 2018 21:44
Show Gist options
  • Save MikeyBurkman/22f46e52be607684f74a8428c3adce5c to your computer and use it in GitHub Desktop.
Save MikeyBurkman/22f46e52be607684f74a8428c3adce5c to your computer and use it in GitHub Desktop.
Deep Exclude Properties (Immutable)
'use strict';
const deepExclude = (node, exclude) => {
if (Array.isArray(node)) {
return node.map((n) => deepExclude(n, exclude));
} else if (typeof node === 'object' && node) {
return Object.entries(node)
.filter(([key]) => exclude.indexOf(key) === -1)
.reduce((obj, [key, value]) => ({ ...obj, [key]: deepExclude(value, exclude) }), {});
}
return node;
};
'use strict';
const o = {
accounts: [{
id: 1,
password: 'abc'
}, {
id: 2,
password: 'qwerty'
}],
users: {
primary: {
name: 'Joe',
email: 'joe@imawesome.com'
}
}
};
console.log(JSON.stringify(deepExclude(o, ['password', 'email']), null, 2));
/*
{
"accounts": [
{
"id": 1
},
{
"id": 2
}
],
"users": {
"primary": {
"name": "Joe"
}
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment