Skip to main content

Loop through an array with a callback function for each iteration.

function loop(array, fn) {
  for (var i = 0; i < array.length; i++) {
    if (fn.call(array, array[i], i) === false) {
      break;
    }
  }
}

// EXAMPLE
var arr = ['12', '34', '56'];

loop(arr, function (item, index) {
  //console.log(this.length);
  console.log(item + ' - ' + index);
});