Suppose we have an object like the following:
likedFoods:{
pizza:true,
pasta:false,
steak:true,
salad:false
}
We want to filter out the false values and convert it into a string array as shown below:
compiledLikedFoods = ["pizza", "steak"]
Is there a more efficient way to achieve this without using multiple if statements like this:
if (this.likedFoods.pizza == true) {this.compiledLikedFoods.push('pizza')};
if (this.likedFoods.pasta == true) {this.compiledLikedFoods.push('pasta')}'
if (this.likedFoods.steak == true) {this.compiledLikedFoods.push('steak')}'
if (this.likedFoods.salad == true) {this.compiledLikedFoods.push('salad')}'
If so, what would be the best approach?
Thank you.