Skip to content

Instantly share code, notes, and snippets.

@vokeio vokeio/each.js
Last active Feb 7, 2017

Embed
What would you like to do?
Loop through each an iterable and enables, break, continue, and manipulation.
function each (iterable, callback, scope) {
var statment = null, i = null, l = null, k = null;
if (iterable.constructor.name === 'Number') {
for (i = 0; i < iterable; i++) {
statment = callback.call(scope, i, iterable);
if (statment === 'break') break;
else if (statment === 'continue') continue;
}
} else if (iterable.constructor.name === 'Map') {
k = iterable.keys();
for (i = 0, l = k.length; i < l; i++) {
statment = callback.call(scope, iterable.get(k[i]), k[i], iterable);
if (statment === 'break') break;
else if (statment === 'continue') continue;
}
} else if (iterable.constructor.name === 'Object') {
for (k in iterable) {
if (!iterable.hasOwnProperty(k)) continue;
statment = callback.call(scope, iterable[k], k, iterable);
if (statment === 'break') break;
else if (statment === 'continue') continue;
}
} else if (iterable.constructor.name === 'Array') {
for (i = 0, l = iterable.length; i < l; i++) {
statment = callback.call(scope, iterable[i], i, iterable);
if (statment === 'break') break;
else if (statment === 'continue') continue;
}
}
return iterable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.