Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save alexhawkins/810b2b1f21223e9e9ff0 to your computer and use it in GitHub Desktop.

Select an option

Save alexhawkins/810b2b1f21223e9e9ff0 to your computer and use it in GitHub Desktop.
Underscore Method Implementations
//#######PLUCK#######
var myPluck = function(obj, propName) {
var arr = [];
for (var i = 0; i < obj.length; i++) {
for (var prop in obj[i]) {
if (prop === propName)
arr.push(obj[i][prop]);
}
}
return arr;
}
//tests
var stooges = [{name: 'moe',age: 40}, {name: 'larry',age: 50}, {name: 'curly',age: 60}];
console.log(myPluck(stooges, 'name')); //[ 'moe', 'larry', 'curly' ]
console.log(myPluck(stooges, 'age')); // [ 40, 50, 60 ]
//#######FILTER#######
var myFilter = function(obj, callback, context){
var arr = [];
obj.forEach(function(el){
if(callback.call(context, el, obj))
arr.push(el);
});
return arr;
};
//tests
var str = ['Zed', 'zeek', 'Zoro', 'god', 'jumanji'];
var newFilter = myFilter(str, function(str){
return str[0] === 'Z';
});
console.log(newFilter); //[ 'Zed', 'Zoro' ]
//#######WHERE#######//FOR ONLY ONE PROPERTY, DOES NOT WORK FOR MULTIPLE
var myWhere = function(obj, objProp) {
//intialize empty array to return
var arr = [],
property, value;
//get property and object we're checking for by
//breaking object passed into individual components
for (var prop in objProp) {
property = prop;
value = objProp[property];
}
//now go through each object and check to see which
//objects have the properties and values we seek
obj.forEach(function(ob) {
for (var prop in ob) {
if (prop === property && ob[prop] === value) {
arr.push(ob);
}
}
});
return arr;
};
//tests
var input = [ { name: 'Tiger', space: 7}, { name: 'Tiger2', space: 1}, { name: 'Tiger3', space: 3}, { name: 'Tiger4', space: 3} ];
var output = myWhere(input, {
space: 3
});
console.log(output); // [ { name: 'Tiger3', space: 3 }, { name: 'Tiger4', space: 3 } ]
var output = myWhere(input, {
space: 7
});
console.log(output); // [ { name: 'Tiger', space: 7 } ]
//#############EACH###################
var myEach = function(obj, callback) {
//first check if object is array
if (typeof obj.length === 'number') {
for (var i = 0; i < obj.length; i++) {
callback(obj[i], i, obj);
}
}
//otherwise we'll assume an Object object was passed
else {
for (var prop in obj) {
callback(obj[prop], prop, obj);
}
}
};
//tests
var arr = [4, 5, 6];
var obj = {
one: 1,
two: 2,
three: 3
};
myEach(arr, function(num) {
console.log(num);
});
myEach(obj, function(num) {
console.log(num);
});
myEach(obj, function(val, key) {
console.log(key + ': ' + val);
});
//#############MAP#####################
var myMap = function(obj, callback) {
var arr = [];
if (typeof obj.length === 'number') {
for (var i = 0; i < obj.length; i++)
arr.push(callback(obj[i], i, obj));
} else {
for (var prop in obj) {
arr.push(callback(obj[prop], prop, obj));
}
}
return arr;
};
//tests
var arr = [100, 23, 33]; // 1 2 3
var obj = {
one: 1,
two: 2,
three: 3
};
//one: 1 two: 2 three: 3
var obj2 = {
title: 'Great Expectations',
author: 'Charles Dickens'
};
var result = myMap(arr, function(num) {
return num * 3;
});
console.log(result); //[ 300, 69, 99 ]
var result = myMap(obj, function(num) {
return num * 3;
});
console.log(result); //[ 3, 6, 9 ]
var result = myMap(obj2, function(val, key) {
return key + ': ' + val;
});
console.log(result); //[ 'title: Great Expectations', 'author: Charles Dickens' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment