Skip to content

Instantly share code, notes, and snippets.

View chriswrightdesign's full-sized avatar

Chris Wright chriswrightdesign

View GitHub Profile
@chriswrightdesign
chriswrightdesign / functionfiveways.js
Last active June 23, 2017 03:42
Function five ways
const valueEquals = value => comparison => value === comparison;
const people = ['bob', 'sarah', 'kate', 'john', 'steve', 'stu', 'sam'];
people.filter(valueEquals('sarah'));
// returns an array value where any value equals sarah
people.find(valueEquals('bob'));
// returns the first value it finds ‘bob’
@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',
},
{