Skip to content

Instantly share code, notes, and snippets.

View chriswrightdesign's full-sized avatar

Chris Wright chriswrightdesign

View GitHub Profile
@chriswrightdesign
chriswrightdesign / arrayfrommap.js
Last active June 23, 2017 03:43
Array.from mapping function example
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];
@chriswrightdesign
chriswrightdesign / drawback.js
Last active June 23, 2017 03:43
drawback - type error
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
@chriswrightdesign
chriswrightdesign / arrayreduce.js
Last active June 23, 2017 06:59
array reduce examples
/**
* 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
@chriswrightdesign
chriswrightdesign / mathroundmap.js
Last active June 23, 2017 03:45
Math.round map
const myValues = [1.34, 3.5, 3.6, 4.1];
myValues.map(Math.round) // [1, 4, 4, 4];
@chriswrightdesign
chriswrightdesign / functionstomap.js
Last active June 23, 2017 06:43
Passing functions to map
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
*/
@chriswrightdesign
chriswrightdesign / functiontomap.js
Last active June 23, 2017 03:44
Function to map
const superSize = value => `super ${value};
animals.map(superSize);
// result: ['super dog', 'super cat', 'super bird'];
@chriswrightdesign
chriswrightdesign / exampleMap.js
Last active June 23, 2017 05:27
Mapping example
const animals = ['dog', 'cat', 'bird', 'giraffe'];
// es5
animals.map(function(currentValue, index, array) {
return 'super ' + currentValue;
});
//es6
animals.map((currentValue, index, array) => `super ${currentValue}`);
@chriswrightdesign
chriswrightdesign / arraymap.js
Last active June 23, 2017 03:46
Array map with function - example for article
users.map(getValue('id'));
// result: ['12345', '34325', '42524']
@chriswrightdesign
chriswrightdesign / ExampleUserList.js
Last active June 23, 2017 06:52
Example list of objects
const getValue = property => obj => obj && obj[property];
// Example List of users
const users = [{
name: 'sarah',
id: '12345',
country: 'Australia',
},
{
@chriswrightdesign
chriswrightdesign / closure-higher-order-example.js
Last active June 23, 2017 05:57
Closure and higher-order function example
// 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];
}
}