Dylan Mac
Dylan Mac

Reputation: 75

How can i filter boolean in object using .filter() method

so i want to filter these objects which is true using the .filter() method but i have no idea how to do it properly. Help me please

    const male = [
        {'mike':true},
        {'james':true},
        {'katy':false},
        {'taylor':false}
    ].filter()

Upvotes: 0

Views: 364

Answers (1)

Phil
Phil

Reputation: 164924

Because you don't know the object key, use Object.values() to get an array of values and use the first entry as the filter

const identifiesAsMale = [
  { 'mike': true },
  { 'james': true },
  { 'katy': false },
  { 'taylor': false }
].filter(o => Object.values(o)[0])

console.log(identifiesAsMale)

This will of course only work if each object has one property with a Boolean value.

Upvotes: 2

Related Questions