Skip to content

Instantly share code, notes, and snippets.

@xeaone
Last active February 7, 2017 22:05
Show Gist options
  • Save xeaone/14f35a0ef782ab3e7bff6821d743f062 to your computer and use it in GitHub Desktop.
Save xeaone/14f35a0ef782ab3e7bff6821d743f062 to your computer and use it in GitHub Desktop.
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