Skip to content

Instantly share code, notes, and snippets.

View apolopena's full-sized avatar
💭
We live to learn!

Apolo Pena apolopena

💭
We live to learn!
View GitHub Profile
@apolopena
apolopena / gist:a22c78a3dbc0bdccbcb455b77c6b8158
Created January 10, 2019 07:18
Javascript Currying Example
// currying example
var add = function(vala){
return function(valb){
return vala + valb;
}
}
console.log(add(2)(2)); // outputs 4
// or more currying just not chained which is the same thing
@apolopena
apolopena / gist:2b6989c85fa7a32e626b83ccade9798e
Last active January 12, 2019 18:45
JavaScript: Get all data in localStorage
function allStorage() {
var archive = [],
keys = Object.keys(localStorage),
i = 0, key;
for (; key = keys[i]; i++) {
archive.push( key + '=' + localStorage.getItem(key));
}
@apolopena
apolopena / JSPluckExample.js
Created January 13, 2019 10:09
JavaScript: plucking values using Array,prototype.map
var cars = [ {make: "Nissan",
model: "Maxima"},
{make: "Infinity",
model: "G60RS"}
];
function pluck(array, property) {
var results = [];
array.map(function(item){
results.push(item[property]);
});
var cars = [{make: "Honda", type: "truck"},{make: "Cadillac", type: "sedan"}, {make: "McLaren", type: "coup"}, {make: "Infinity", type: "coup"}];
function reject(array, iteratorFunction) {
return array.filter(iteratorFunction);
}
var noCoups = reject(cars, function(car) {
return car.type !== "coup";
});
// or numbers reusing 'filter' to reverse the effect of filter (only push items that return false)
var numbers = [10,20,30];
@apolopena
apolopena / JSFilterFindWrapperExample.js
Created January 13, 2019 20:54
JavaScript: Array.prototype.find() wrapper example
var ladders= [{height: '20 feet'}, {height: '10 feet'},{height: '100 feet'}];
function findWhere(array, criteria) {
return array.find(function(item){
return item.height === criteria.height;
});
}
var twentyFootLadder = findWhere(ladders, {height: '20 feet'});
@apolopena
apolopena / JSSimpleReduceExamples.js
Last active January 13, 2019 23:34
JavaScript: Simple Reduce Examples
var miles = [{ distance: 34 }, { distance: 12 } , { distance: 1 }];
var totalDistance = miles.reduce(function(prev, item){return prev + item.distance;}, 0);
//return the total of sitting and standing chairs
var desks = [
{ type: 'sitting' },
{ type: 'standing' },
{ type: 'sitting' },
{ type: 'sitting' },
{ type: 'standing' }
@apolopena
apolopena / JSUniqueNumArrayWithReduceAndFind.js
Created January 14, 2019 01:44
JavaScript: return an array of unique numbers from an array of numbers using reduce() and find()
function makeUnique(array) {
return array.reduce(function(prev, num) {
if (!prev.find(function(item) { return item == num; })) {
prev.push(num);
}
return prev;
}, []);
}
var uniqueNumbers = makeUnique([1, 6 ,5, 6, 4, 3, 3, 3, 2, 1, 1]);
@apolopena
apolopena / JSDestructureArrayofArraysToObjExample.js
Created January 15, 2019 09:17
JavaScript: es6 destructuring an array of point arrays to and array of x, y location objects
const points = [
[1, 2],
[50, -10],
[110, 405]
];
points.map(([x, y]) => {
return { x, y };
});
@apolopena
apolopena / JSRecursiveDoubleArrayWithRestAndSpread.js
Last active January 15, 2019 22:45
JavaScript: double an array of number with a one-liner using rest, spread and recursion
function double([number, ...rest]) {
return !number ? [] : [number * 2, ...double(rest)];
}
@apolopena
apolopena / JSArrayMapForScore.js
Created January 18, 2019 05:05
JavaScript: compare array and return a score using array.map
function compareTriplets(a, b) {
let score1, score2, iter;
score1 = score2 = iter = 0;
a.map(item => {
if (item > b[iter]) {
score1++;
}
if ( item < b[iter]) {
score2++;
}