Last active
December 7, 2018 21:44
-
-
Save MikeyBurkman/22f46e52be607684f74a8428c3adce5c to your computer and use it in GitHub Desktop.
Deep Exclude Properties (Immutable)
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
'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; | |
}; |
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
'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