I have a large javascript object which contains multiple instances of a key. I'd like to remove all instances of this key and value from the object.
I have this function which allows me to find a nested object, though I'm not sure how to modify it to get all objects, and to delete the keys from the object. Can someone help me with this?
var findObjectByKey= function (o, key) {
if (!o || (typeof o === 'string')) {
return null
}
if (o[key]) {
return o[key];
}
for (var i in o) {
if (o.hasOwnProperty(i)) {
var found = findObjectByKey(o[i], key)
if (found) {
return found
}
}
}
return null
}
This is an example object where I'd like to remove all keys d
:
var a = {
b: {
c: {
d: 'Hello',
},
d: 'Hola',
e: {
f: {
d: 'Hey'
}
}
}
}
// To become
var a = {
b: {
c: {},
e: {
f: {}
}
}
}
Also, is there a way we could do this with Ramda by chance?