I have the following JS object:
let obj = {
'a': 1,
'a-gaboom': 1,
'b': 1,
'b-gaboom': 1
};
I want to delete all fields that end with '-gaboom'
.
I could do it manually with:
delete obj['a-gaboom'];
delete obj['b-gaboom'];
But I'd like to do it dynamically using a RegExp?
I have the following JS object:
let obj = {
'a': 1,
'a-gaboom': 1,
'b': 1,
'b-gaboom': 1
};
I want to delete all fields that end with '-gaboom'
.
I could do it manually with:
delete obj['a-gaboom'];
delete obj['b-gaboom'];
But I'd like to do it dynamically using a RegExp?
Share edited Jan 2, 2020 at 22:33 Danziger 21.2k6 gold badges58 silver badges89 bronze badges asked Jan 2, 2020 at 21:56 gkeenleygkeenley 7,47817 gold badges78 silver badges180 bronze badges 1-
The current answers each assume that all
-gaboom
s are at the root. If you need to turn{a: 1, b: {'c-gaboom': 2, d: 3, e: {f: 5, 'g-gaboo,': 6}}}
into{a: 1, b: {d: 3, e: {f: 5}}}
then it will still not be difficult, but is itself an interesting question. – Scott Sauyet Commented Feb 17, 2022 at 14:09
3 Answers
Reset to default 5You could do it using String.prototype.includes()
or String.prototype.indexOf()
, even though these will not take into account if -gaboom
appears at the end of the key or at any other position, or using String.prototype.endsWith()
, which will make sure the match is at the end of the key:
const obj = {
'a': 1,
'a-gaboom': 1,
'b': 1,
'b-gaboom': 1
};
for (const key in obj) {
if (key.endsWith('-gaboom')) delete obj[key];
}
console.log(obj);
Anyway, this would be how it would look like with a RegExp, which does make sure -gaboom
is at the end of the key by using $
and can easily be adapted to be way more flexible than any of the other alternatives above:
const obj = {
'a': 1,
'a-gaboom': 1,
'b': 1,
'b-gaboom': 1
};
for (const key in obj) {
if (/-gaboom$/.test(key)) delete obj[key];
}
console.log(obj);
If you want to go for something more readable or a one-liner, I'd go for @OriDrori's approach or something similar using Array.prototype.reduce()
:
const obj = {
'a': 1,
'a-gaboom': 1,
'b': 1,
'b-gaboom': 1
};
const filtered = Object.keys(obj).filter(key => !/-gaboom$/.test(key)).reduce((acc, key) => {
acc[key] = obj[key];
return acc;
}, { });
console.log(filtered);
You can get the entries (array of [key, value] pairs) of the object with Object.entries()
. Filter the array of entries, and keep only entries with a key that doesn't end with -gaboom
. Convert back to an object via Object.fromEntries()
:
const obj = {'a': 1,'a-gaboom': 1,'b': 1,'b-gaboom': 1}
const result = Object.fromEntries(
Object.entries(obj)
.filter(([key]) => !key.endsWith('-gaboom'))
)
console.log(result)
taking a cue from the other answers, with this generic function it is possible to eliminate all the keys that verify a specific condition while mutating the original object.
deleteKeys = function (obj, fn) {
Object.entries(obj)
.filter(([key, value]) => fn(key, value))
.forEach(([key]) => delete obj[key]);
};
const obj = {'a': 1,'a-gaboom': 1,'b': 1,'b-gaboom': 1};
deleteKeys(obj, (key) => !key.endsWith("-gaboom"));
console.log(obj)