Scenario:
Assume we have the following object
ingredients = { 'pizza': false, 'basil': true, 'olives': false }
Requirement:
Filter object based on its values using ES6/JS. We need to get the filter the above object which has value set to true.
Solution:
The object can be filtered using filter and map functions.
let filteredIngredients = Object.keys(ingredients)
.filter((value) => {
return ingredients[value] == true;
}).map((ingredient, index) => {
return ingredient;
});
console.log(filteredIngredients);
Output:
["pizza", "basil"]
Assume we have the following object
ingredients = { 'pizza': false, 'basil': true, 'olives': false }
Requirement:
Filter object based on its values using ES6/JS. We need to get the filter the above object which has value set to true.
Solution:
The object can be filtered using filter and map functions.
let filteredIngredients = Object.keys(ingredients)
.filter((value) => {
return ingredients[value] == true;
}).map((ingredient, index) => {
return ingredient;
});
console.log(filteredIngredients);
Output:
["pizza", "basil"]