trying to use underscore.map to iterate over array of objects and add a boolean property to each object based on if the object value passes a truth statement.
var desserts = [
{
name: 'Chocolate Cake',
ingredients: ['cocoa', 'flour', 'sugar', 'eggs', 'milk', 'butter' ],
type: 'cake'
},
{
name: 'Snickerdoodles',
ingredients: ['flour', 'milk', 'butter', 'eggs', 'sugar', 'cinnamon', 'cream of tartar'],
type: 'cookie'
},
etc..
Heres what I have -
var glutenFree = function (desserts) {
return _.map(desserts, function(dessert) {
if (dessert.ingredients.includes('flour')) {
return dessert.glutenFree = 'false';
} else {
return dessert.glutenFree = 'true';
}
});
};
No library needed:
const desserts = [{
name: 'Chocolate Cake',
ingredients: ['cocoa', 'flour', 'sugar', 'eggs', 'milk', 'butter'],
type: 'cake'
},
{
name: 'Snickerdoodles',
ingredients: ['flour', 'milk', 'butter', 'eggs', 'sugar', 'cinnamon', 'cream of tartar'],
type: 'cookie'
}
];
const dessertsExtended = desserts.map(dessert => ({ ...dessert,
glutenfree: !dessert.ingredients.includes('flour')
}));
console.log(dessertsExtended);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With