Chris
Chris

Reputation: 155

Fire event based on object value

I have an object like

let obj = { 
        1: true, 
        2:false,
        3:true
}

How can i return the object key wherever the object value is false, for example, in the case above, only 2 should be returned

I tried Object.values(obj).filter(value => !value) but it only returns false

Upvotes: 2

Views: 32

Answers (2)

Maheer Ali
Maheer Ali

Reputation: 36594

You are using filter() on Object.values() this way you will not have any way to access the corresponding key

You can use filter() on Object.keys() and check if value at that key is truthy.

let obj = { 
        1: true, 
        2:false,
        3:true
}
const res = Object.keys(obj).filter(k => !obj[k]);
console.log(res)

Upvotes: 1

Chris
Chris

Reputation: 155

Actually, came up with

Object.entries(obj).filter(([key, value]) => !value).map(([key, value]) => key);

which returns what I need.

Upvotes: 1

Related Questions