Last active
June 29, 2016 21:47
-
-
Save frankjdelgado/a1ee2091b73ef4b9c8edf5f0890f61f6 to your computer and use it in GitHub Desktop.
Javascript array custom functions
This file contains 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
/* | |
* 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