Last active
July 7, 2021 15:23
-
-
Save JoeyBurzynski/91e0ea5f480f5f6bfbcca5df832225b7 to your computer and use it in GitHub Desktop.
JavaScript: Higher Order Functions, Part 2 (Map by Example) / YouTube Video: http://uzr.co/javascript-map-method
This file contains hidden or 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
/* | |
JavaScript: Higher Order Functions | |
Part 2, JavaScript Map Method by Example | |
YouTube Video: http://uzr.co/javascript-map-method | |
JSFiddle: https://jsfiddle.net/joeyburzynski/j2bewfe9/ | |
Gist: https://gist.github.com/JoeyBurzynski/91e0ea5f480f5f6bfbcca5df832225b7 | |
See Part 1, JavaScript Filter Method by Example at: | |
YouTube Video: http://uzr.co/javascript-filter-method | |
JSFiddle: https://jsfiddle.net/joeyburzynski/5wedpzrw/ | |
Gist: https://gist.github.com/JoeyBurzynski/5bdacb580e2d52e51b5b7a512d404cf3 | |
See Part 3, JavaScript Reduce Method by Example at: | |
YouTube Video: http://uzr.co/javascript-reduce-method | |
JSFiddle: https://jsfiddle.net/joeyburzynski/fv87pLs3/ | |
Gist: https://gist.github.com/JoeyBurzynski/691a2ca6f5e6ab834e306691e91634aa | |
See Part 4, JavaScript Reduce Method (Advanced Usage) by Example at: | |
YouTube Video: http://uzr.co/jav ascript-reduce-method-advanced-usage | |
JSFiddle: https://jsfiddle.net/joeyburzynski/ws2j6f8x/ | |
Gist: https://gist.github.com/JoeyBurzynski/25be414b75070028905e9c3b21702824 | |
See Part 5, JavaScript Closures by Example at: | |
YouTube Video: http://uzr.co/javascript-closures | |
JSFiddle: https://jsfiddle.net/joeyburzynski/u6m541oo/ | |
Gist: https://gist.github.com/JoeyBurzynski/c532013a96da1dd88eb58151514ec057 | |
See Part 6, JavaScript Currying by Example at: | |
YouTube Video: http://uzr.co/javascript-currying | |
JSFiddle: https://jsfiddle.net/joeyburzynski/u1njcre2/ | |
Gist: https://gist.github.com/JoeyBurzynski/d57687b14a6953b9d349669dad871647 | |
See Part 7, Recursion by Example at: | |
YouTube Video: http://uzr.co/javascript-recursion | |
JSFiddle: https://jsfiddle.net/joeyburzynski/gn6pndr8/ | |
Gist: https://gist.github.com/JoeyBurzynski/dac240b01e6c2994440afcd4d73218df | |
See Part 8, Promises by Example at: | |
YouTube Video: http://uzr.co/javascript-promises-by-example | |
JSFiddle: https://jsfiddle.net/joeyburzynski/5vjcq9fL/ | |
Gist: https://gist.github.com/JoeyBurzynski/c356e23d0daf6910e3ee8cbd919f0acb | |
*/ | |
// Create an array of animals for use below: | |
var animals = [{ | |
name: "Fluffykins", | |
species: 'rabbit' | |
}, { | |
name: "Caro", | |
species: 'dog' | |
}, { | |
name: "Hamilton", | |
species: 'dog' | |
}, { | |
name: "Harold", | |
species: 'fish' | |
}, { | |
name: "Ursula", | |
species: 'cat' | |
}, { | |
name: "Jimmy", | |
species: 'fish' | |
}]; | |
// ES5 For Loop Syntax | |
// Standard for loop syntax for iterating over the animals array and pushing all animal names into "names" array. | |
var names = []; // Initialize empty array for collecting animal names | |
for (var i = 0; i < animals.length; i++) { | |
names.push(animals[i].name); | |
} | |
console.log(names); // ["Fluffykins", "Caro", "Hamilton", "Harold", "Ursula", "Jimmy"] | |
// ES5 Map Example | |
// It will iterate over all items in the array, but allows the callback function to transform the object. | |
var names = animals.map(function(animal) { | |
return animal.name; | |
}); | |
console.log(names); // ["Fluffykins", "Caro", "Hamilton", "Harold", "Ursula", "Jimmy"] | |
// ES6 Map Syntax (Arrow Functions) | |
// Shorter syntax to achieve the same. | |
var names = animals.map((animal) => animal.name) | |
// ES6 Map Syntax (Arrow Functions) | |
// Even shorter: | |
var names = animals.map((x) => x.name) | |
console.log(names); // ["Fluffykins", "Caro", "Hamilton", "Harold", "Ursula", "Jimmy"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment