Reputation: 75
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
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