Skip to content

Instantly share code, notes, and snippets.

@rao123dk
Created April 6, 2018 06:26
Show Gist options
  • Save rao123dk/11d0a871102f621a00259ed8b85fb3cd to your computer and use it in GitHub Desktop.
Save rao123dk/11d0a871102f621a00259ed8b85fb3cd to your computer and use it in GitHub Desktop.
Own for each for array in JavaScript.
var array_list = [1,2,3,4,5,3];
Array.prototype.raoEach = function(cb){
for (i = 0; i < this.length; i++) {
cb(this[i]);
}
}
array_list.raoEach(function(a){
console.log(a+2);
});
@rao123dk
Copy link
Author

rao123dk commented Apr 6, 2018

This is updated code for also return index position.

var array_list = [1,2,3,4,5,3];
Array.prototype.raoEach = function(cb){
for (i = 0; i < this.length; i++) {
cb(this[i] , i);
}
}

array_list.raoEach(function(a,index){
console.log(a+2 , index);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment