filter()
`Array.prototype.filter()`
"*The `filter()` method creates a new array with all elements that pass the test implemented by the provided function.*" - **Official MDN Web docs**
const food = ['🍩', '🍪', '🍪', '🥕', '🥐'];
filter()
const food = ['🍩', '🍪', '🍪', '🥕', '🥐'];
const onlyCookies = food.filter();
filter()
const food = ['🍩', '🍪', '🍪', '🥕', '🥐'];
const onlyCookies = food.filter(x => x === '🍪');
filter()
// Output of console.log(onlyCookies);
['🍪', '🍪' ]
filter()
map()
`Array.prototype.map()`
"*The `map()` method creates a new array with the results of calling a provided function on every element in the calling array.*" - **Official MDN Web docs**
Tell me again, only tastier...
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
map()
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const jumboMarshmallows = bagOfMarshmallows.map();
map()
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const jumboMarshmallows = bagOfMarshmallows.map(marshmallow => {
return marshmallow.toUpperCase();
});
map()
// Output of console.log(jumboMarshmallows);
[ 'MARSHMALLOW',
'MARSHMALLOW',
'MARSHMALLOW',
'MARSHMALLOW',
'MARSHMALLOW',
'☁' ]
map()
Sweet. Jumbo MARSHMALLOWS are the best!
But wait... what's that ☁ doing there?
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const jumboMarshmallows = bagOfMarshmallows.map(marshmallow => {
return marshmallow.toUpperCase();
});
map()
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const jumboMarshmallows = bagOfMarshmallows.map(marshmallow => {
return marshmallow.toUpperCase();
});
map()
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const jumboMarshmallows = bagOfMarshmallows
.map(marshmallow => {
return marshmallow.toUpperCase();
});
map()
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const jumboMarshmallows = bagOfMarshmallows
.map(marshmallow => {
return marshmallow.toUpperCase();
});
map()
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const filteredJumboMarshmallows = bagOfMarshmallows
.map(marshmallow => {
return marshmallow.toUpperCase();
});
map()
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const filteredJumboMarshmallows = bagOfMarshmallows
// Filter out any random clouds first!
.map(marshmallow => {
return marshmallow.toUpperCase();
});
map()
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const filteredJumboMarshmallows = bagOfMarshmallows
.filter(x => x !== '☁')
.map(marshmallow => {
return marshmallow.toUpperCase();
});
map()
const bagOfMarshmallows = [
'marshmallow',
'marshMallow',
'MarshMallow',
'MARSHmallow',
'marshMALLOW',
'☁'
];
const filteredJumboMarshmallows = bagOfMarshmallows
.filter(x => x !== '☁')
.map(marshmallow => {
return marshmallow.toUpperCase();
});
map()
// Output of console.log(filteredJumboMarshmallows);
[ 'MARSHMALLOW',
'MARSHMALLOW',
'MARSHMALLOW',
'MARSHMALLOW',
'MARSHMALLOW']
map()
reduce()
`Array.prototype.reduce()`
"*The `reduce()` method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.*" - **Official MDN Web docs**
That's a mouthful... (and not the sweet kind.)
const donutLog = [
{ day: 'Monday', totalEaten: 2 },
{ day: 'Tuesday', totalEaten: 1 } ,
{ day: 'Wednesday', totalEaten: 3 },
{ day: 'Thursday', totalEaten: 2 },
{ day: 'Friday', totalEaten: 4 }
];
reduce()
const donutLog = [
{ day: 'Monday', totalEaten: 2 },
{ day: 'Tuesday', totalEaten: 1 },
{ day: 'Wednesday', totalEaten: 3 },
{ day: 'Thursday', totalEaten: 2 },
{ day: 'Friday', totalEaten: 4 },
];
const caloriesConsumed = donutLog.reduce();
reduce()
const donutLog = [
{ day: 'Monday', totalEaten: 2 },
{ day: 'Tuesday', totalEaten: 1 },
{ day: 'Wednesday', totalEaten: 3 },
{ day: 'Thursday', totalEaten: 2 },
{ day: 'Friday', totalEaten: 4 },
];
const caloriesConsumed = donutLog.reduce((accumulator, currentValue)
);
reduce()
const donutLog = [
{ day: 'Monday', totalEaten: 2 },
{ day: 'Tuesday', totalEaten: 1 },
{ day: 'Wednesday', totalEaten: 3 },
{ day: 'Thursday', totalEaten: 2 },
{ day: 'Friday', totalEaten: 4 },
];
const caloriesConsumed = donutLog.reduce((lastCount, currentEntry) => {
return lastCount + (currentEntry.totalEaten * 278);
}, 0);
reduce()
// Output of console.log(caloriesConsumed);
3336
reduce()
const menu = [
{
name: 'Fresh Strawberry',
emoji: '🍓',
calories: 453,
price: 1.45
},
{
name: 'Espresso',
emoji: '☕',
calories: 376,
price: 1.45
},
{
name: 'Chocolate Iced',
emoji: '🍩',
calories: 273,
price: 1.45
},
{
name: 'Salted Caramel',
emoji: '🍬',
calories: 492,
price: 1.55
},
{
name: 'Sweet Coconut',
emoji: '🥥',
calories: 391,
price: 1.55
},
];
reduce()
const order = ['🍓', '☕', '🍩', '🍩', '🍬', '🥥'];
const RIC_COMBINED_SALES_TAX = 0.053;
reduce()
// Get receipt items ready
const receiptItems = menu
.filter((food) => order.includes(food.emoji))
.map((item) => {
const numberOrdered = order.filter(x => x === item.emoji).length;
return {
emoji: item.emoji,
price: item.price * numberOrdered,
numberOrdered: numberOrdered
};
});
reduce()
const totals = receiptItems
.reduce((totals, { price }) => {
const subtotal = totals.subtotal + price;
const salesTax = totals.salesTax + price * RIC_COMBINED_SALES_TAX;
return {
subtotal,
salesTax
};
}, { subtotal: 0, salesTax: 0 });
reduce()
const receipt = 'Receipt - Adrienne - Sugar Shack Donuts\n' +
receiptItems.map((x) => {
return `${x.numberOrdered} ${x.emoji} \t\t$${x.price.toFixed(2)}`
}).join('\n') + '\n' +
`----------------------\n` +
`Sub Total \t$${totals.subtotal.toFixed(2)}\n` +
`Sales Tax \t$${totals.salesTax.toFixed(2)}\n` +
`-----------------------\n` +
`Order Total \t$${(totals.subtotal + totals.salesTax).toFixed(2)}`;
reduce()
console.log(receipt);
reduce()
Receipt - Adrienne - Sugar Shack
1 🍓 $1.45
1 ☕ $1.45
2 🍩 $2.90
1 🍬 $1.55
1 🥥 $1.55
-----------------------
Sub Total $8.90
Sales Tax $0.47
-----------------------
Order Total $9.37
reduce()
Salamat! // That means 'Thank you' in Tagalog!
Find me here
Twitter: @adriennetacke Blog: blog.adrienne.io
Delicious JavaScript: Delectable Explanations of the Power of JS