How to remove the key of an object knowing its value?

I have a object
var obj = {
 key1: "value1",
 key2: "value1",
 key3: "value2",
 key4: "value3"
}


You need to remove all keys with a value of value1 to get the object
var obj = {
 key3: "value2",
 key4: "value3"
}
March 19th 20 at 08:28
1 answer
March 19th 20 at 08:30
Solution
Object.entries(obj).forEach(n => n[1] === 'value1' && delete obj[n[0]])

or, if you really want to remove properties from an existing object, and collect new:

Object.entries(obj).reduce((acc, [ k, v ]) => (v !== 'value1' && (acc[k] = v), acc), {})
// or
Object.fromEntries(Object.entries(obj).filter(n => n[1] !== 'value1'))

Find more questions by tags JavaScript