Skip to content

Instantly share code, notes, and snippets.

@neyosoft
Forked from Billy-/README.md
Created May 1, 2018 09:09
Show Gist options
  • Save neyosoft/1ea4f0657499c01a37dbc68218223137 to your computer and use it in GitHub Desktop.
Save neyosoft/1ea4f0657499c01a37dbc68218223137 to your computer and use it in GitHub Desktop.
deep version of lodash omit
function omitDeep(obj, key) {
if (Array.isArray(obj)) return omitDeepArrayWalk(obj, key)
const keys = Object.keys(obj)
const newObj = {}
keys.forEach((i) => {
if (i !== key) {
const val = obj[i]
if (Array.isArray(val)) newObj[i] = omitDeepArrayWalk(val, key)
else if (typeof val === 'object' && val !== null) newObj[i] = omitDeep(val, key)
else newObj[i] = val
}
})
return newObj
}
function omitDeepArrayWalk(arr, key) {
return arr.map((val) => {
if (Array.isArray(val)) return omitDeepArrayWalk(val, key)
else if (typeof val === 'object') return omitDeep(val, key)
return val
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment