Last active
January 26, 2019 03:10
-
-
Save ankryption/3a41d280cf03c26e9a82d2cbcdb29da4 to your computer and use it in GitHub Desktop.
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
/* | |
* A simple function that returns the objects between a range of given values, and grouped by a given key | |
*/ | |
Array.prototype.customFilter = function(group, range, low, high) { | |
if (!(array instanceof Array)) { | |
throw new Error("Invalid array"); | |
} | |
var result = {}; | |
this.forEach(function(item) { | |
if (result && group && item[group] && !result[item[group]]) { | |
result[item[group]] = []; | |
} | |
if (item && range && group && item[range] && item[group] && result[item[group]]) { | |
if (item[range] >= low && item[range] <= high) { | |
result[item[group]].push(item); | |
} | |
} | |
}); | |
return result; | |
} | |
var array = [{name: 'Ankush', age: 26, gender: 'M'}, {name: 'Sakshi', age: 36, gender: 'F'}, {name: 'Mayank', age: 38, gender: 'M'}, {name: 'Akshay', age: 35, gender: 'M'}, {name: 'Anita', age: 36, gender: 'F'}, {name: 'Rakesh', age: 52, gender: 'M'}]; | |
array.customFilter('gender', 'age', 30, 40); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment