Skip to content

Instantly share code, notes, and snippets.

@ianpgall
Last active December 21, 2015 04:09
Show Gist options
  • Save ianpgall/6247244 to your computer and use it in GitHub Desktop.
Save ianpgall/6247244 to your computer and use it in GitHub Desktop.
JavaScript function to slice an Array-like object (same arguments as native Array.slice)
var Slice = (function () {
"use strict";
var func;
func = function (arr, start, howMany) {
var stop, newArr, i, j, cur;
start = +start || 0;
howMany = +howMany || Infinity;
stop = Math.min(start + howMany, arr.length);
newArr = [];
for (i = start, j = stop; i < j; i++) {
cur = arr[i];
newArr.push(cur);
}
return newArr;
};
return func;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment