-
-
Save aurbano/383e691368780e7f5c98 to your computer and use it in GitHub Desktop.
/** | |
* Remove all specified keys from an object, no matter how deep they are. | |
* The removal is done in place, so run it on a copy if you don't want to modify the original object. | |
* This function has no limit so circular objects will probably crash the browser | |
* | |
* @param obj The object from where you want to remove the keys | |
* @param keys An array of property names (strings) to remove | |
*/ | |
function removeKeys(obj, keys){ | |
var index; | |
for (var prop in obj) { | |
// important check that this is objects own property | |
// not from prototype prop inherited | |
if(obj.hasOwnProperty(prop)){ | |
switch(typeof(obj[prop])){ | |
case 'string': | |
index = keys.indexOf(prop); | |
if(index > -1){ | |
delete obj[prop]; | |
} | |
break; | |
case 'object': | |
index = keys.indexOf(prop); | |
if(index > -1){ | |
delete obj[prop]; | |
}else{ | |
removeKeys(obj[prop], keys); | |
} | |
break; | |
} | |
} | |
} | |
} |
That will only remove nested Strings or Objects from objects...
> obj = { deleteme: true }
{ deleteme: true }
> removeKeys(obj, 'deleteme')
undefined
> obj
{ deleteme: true }
What you may want is:
function removeKeys(obj, keys) {
for (var prop in obj) {
if(obj.hasOwnProperty(prop)) {
switch(typeof(obj[prop])) {
case 'object':
if(keys.indexOf(prop) > -1) {
delete obj[prop];
} else {
removeKeys(obj[prop], keys);
}
break;
default:
if(keys.indexOf(prop) > -1) {
delete obj[prop];
}
break;
}
}
}
}
Hmm.. I want to remove all circular references in an object 😞 . I thought it was possible 🥇
Just for future reference
const removeKey = (obj, key) => obj !== Object(obj)
? obj
: Array.isArray(obj)
? obj.map(item => removeKey(item, key))
: Object.keys(obj)
.filter(k => k !== key)
.reduce((acc, x) => Object.assign(acc, { [x]: removeKey(obj[x], key) }), {});
Thanks @mac2000 I would just changed it to have multiple keys, with an includes
check :
const removeKeys = (obj, keys) => obj !== Object(obj)
? obj
: Array.isArray(obj)
? obj.map((item) => removeKeys(item, keys))
: Object.keys(obj)
.filter((k) => !keys.includes(k))
.reduce(
(acc, x) => Object.assign(acc, { [x]: removeKeys(obj[x], keys) }),
{}
)
Hey everyone, I recently put this together using the example above. Admittedly, I personally have trouble following .reduce's logic from time to time, so it helps to distill it down for myself. Wanted to check with you guys to see if you see anything wrong with my logic:
const removeKeys = (obj, keys) => obj !== Object(obj)
? obj
: Array.isArray(obj)
? obj.map((item) => removeKeys(item, keys))
: Object.fromEntries(Object.entries(obj).filter(([k]) => !keys.includes(k)));
@mac2000 that's a nice solution except for every object you remove it is replaced with an empty object, so if i have an array of objects, and i want to remove one of them from the array it is replaced with an empty object.
Here is a variant of yours.
const obj = {'a': {'b': {'c': {'a': 1}, 'B':1}, 'd':{'e':1, 'a':1}},'c':123}
const key = 'a.b.c';
removeKey(obj, key.split('.'))
will give you the result of
'{"a":{"b":{"B":1},"d":{"e":1,"a":1}},"c":123}'
could be useful if you want to remove a property that is at the specific location.