Skip to content

Instantly share code, notes, and snippets.

@brettz9
Forked from WebReflection/each.js
Created August 28, 2013 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettz9/6367524 to your computer and use it in GitHub Desktop.
Save brettz9/6367524 to your computer and use it in GitHub Desktop.
var each = function (Array) {"use strict";
/*! all you need to `each(obj, cb [,context])`
* @author WebReflection
* @license WTFPL - http://en.wikipedia.org/wiki/WTFPL
* @gist https://gist.github.com/2294934
*/
var
toString = {}.toString,
hasOwnProperty = {}.hasOwnProperty,
array = toString.call([]),
NL = typeof NodeList != "undefined" ? NodeList : Array,
isArrayLike = function isArrayLike(obj) {
return obj != null &&
(
obj instanceof Array ||
obj instanceof NL
) ||
toString.call(obj) == array ||
(
(
(t = typeof obj) != "function" &&
t != "string"
) &&
typeof(l = obj.length) == "number" &&
l === ~~l
)
;
},
forEach = [].forEach || function forEach(callback, context) {
for (var
obj = this, i = 0, length = obj.length;
i < length;
++i
)
i in obj && callback.call(context, obj[i], i, obj)
;
},
t, l
;
return function each(obj, callback, context) {
if (isArrayLike(obj))
forEach.call(obj, callback, context)
;
else for (var key in obj)
hasOwnProperty.call(obj, key) && callback.call(context, obj[key], key, obj)
;
};
}(Array);
@brettz9
Copy link
Author

brettz9 commented Mar 29, 2018

Would probably use Object.entries(...).forEach now

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