Inspired by this issue on the lodash repo, asking for deep functionality for omit.
Note that there are plans to remove omit entirely in lodash v5... and props to this comment (and the one it quotes) in response to that 😆.
Inspired by this issue on the lodash repo, asking for deep functionality for omit.
Note that there are plans to remove omit entirely in lodash v5... and props to this comment (and the one it quotes) in response to that 😆.
| function omitDeep(value, key) { | |
| if (Array.isArray(value)) { | |
| return value.map(i => omitDeep(i, key)) | |
| } | |
| else if (typeof value === 'object' && value !== null) { | |
| return Object.keys(value) | |
| .reduce( | |
| (newObject, k) => { | |
| if (k == key) return newObject | |
| return Object.assign( | |
| { [k]: omitDeep(value[k], key) }, | |
| newObject | |
| ) | |
| }, | |
| {} | |
| ) | |
| } | |
| return value | |
| } |
Important:
because of typeof null === 'object' line 8 should state:
else if (typeof val === 'object' && val !== null) newObj[i] = omitDeep(val, key)
Thanks for catching that, I've updated the gist.
Thanks for quick solution