List of utility methods to do Array Search
searchById(id)Array.prototype.searchById = function(id){ for(var i=0; i<this.length; i++){ if(this[i].id === id){ return this[i]; }; | |
searchIndex(n)"use strict"; Array.prototype.searchIndex = function(n){ var result = [], i, l= this.length; for( i= 0; i< l; i++){ if(this[i] == n){ result.push(i); if(result.length == 0){ result.push(-1); return result; }; var arr = [1,2,5,5,5,5,5,6,9]; console.log(arr.searchIndex(5)); console.log(arr.searchIndex(0)); | |
searchObject(filterParams)'use strict'; Array.prototype.searchObject = function (filterParams) { return this.filter(function (v) { return recursiveMatchFunc(v, filterParams); }); }; Array.prototype.randomSelect = function () { return this[Math.floor(Math.random() * this.length)]; }; ... |