This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.from({length: 3}); | |
// result: [undefined, undefined, undefined] | |
// Array.from's second argument is a mapping function. | |
Array.from({length: 3}, (currentValue, index) => index); | |
// result: [0, 1, 2]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const prefix = (prefix, value) => `${prefix}${value}`; | |
prefix('super ', 'dog'); // 'super dog'; | |
// We can’t use our new prefix function inside of the array methods. | |
animals.map(prefix('super '); | |
// Result: TypeError: undefined super is not a function | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Numbers: | |
* We can reduce an array of numbers, to find the total. Using a number as initial value | |
**/ | |
const numbers = [5, 10, 25, 40]; | |
numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0); // result: 80 | |
// if we pass 10 as the initial value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const myValues = [1.34, 3.5, 3.6, 4.1]; | |
myValues.map(Math.round) // [1, 4, 4, 4]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const myAnimals = ['dog', undefined, 'cat', 'bird', undefined, 0, false]; | |
myAnimals.filter(Boolean); // Result: ['dog', 'cat', 'bird']; | |
/* | |
it will call Boolean('dog'), then Boolean(undefined), | |
then Boolean('cat') etc etc, the array.filter() callback must return true to keep | |
an element in the array, if its false it excludes it | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const superSize = value => `super ${value}; | |
animals.map(superSize); | |
// result: ['super dog', 'super cat', 'super bird']; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const animals = ['dog', 'cat', 'bird', 'giraffe']; | |
// es5 | |
animals.map(function(currentValue, index, array) { | |
return 'super ' + currentValue; | |
}); | |
//es6 | |
animals.map((currentValue, index, array) => `super ${currentValue}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
users.map(getValue('id')); | |
// result: ['12345', '34325', '42524'] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getValue = property => obj => obj && obj[property]; | |
// Example List of users | |
const users = [{ | |
name: 'sarah', | |
id: '12345', | |
country: 'Australia', | |
}, | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example of a higher order function: .map() on arrays, map expects a function callback | |
[1,2,3].map(num => num + 1); // [2, 3, 4]; | |
// closures in es5 syntax | |
function getValue(property) { | |
return function(obj) { | |
return obj && obj[property]; | |
} | |
} |