Skip to content

Instantly share code, notes, and snippets.

@frankjdelgado
Last active June 29, 2016 21:47
Show Gist options
  • Save frankjdelgado/a1ee2091b73ef4b9c8edf5f0890f61f6 to your computer and use it in GitHub Desktop.
Save frankjdelgado/a1ee2091b73ef4b9c8edf5f0890f61f6 to your computer and use it in GitHub Desktop.
Javascript array custom functions
/*
* Returns an array that contains the indexes from a search that match the array values with the given regular expression
* @param str Regular expresion
* return Array of indexes (integers), -1 if no value matches the regexp
*/
Array.prototype.allIndexOfStr = function (str) {
var pos = [];
for (var j=0; j<this.length; j++) {
if (this[j].match(str))
pos.push(j);
}
return pos.length > 0 ? pos : -1;
}
/*
* Remove position(s) from an array
* @param array/integer List of indexes/index to remove
* return original array with removed positions
*/
Array.prototype.remove = function(ind){
if(ind.constructor === Array){
var n = ind.length;
for(var i = 0; i < n ; i++){
this.splice(ind[i]-i,1);
}
}else{
this.splice(ind,1);
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment