2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-21 13:24:11 +03:00
Files
axios/lib/forEach.js
T
2014-08-27 01:06:19 -06:00

34 lines
910 B
JavaScript

'use strict';
/**
* Iterate over an Array or an Object invoking a function for each item.
*
* If `obj` is an Array or arguments callback will be called passing
* the value, index, and complete array for each item.
*
* If 'obj' is an Object callback will be called passing
* the value, key, and complete object for each property.
*
* @param {Object|Array} obj The object to iterate
* @param {Function} fn The callback to invoke for each item
*/
module.exports = function forEach(obj, fn) {
if (typeof obj !== 'object') {
return;
}
// Iterate over array values
if (obj.constructor === Array || typeof obj.callee === 'function') {
for (var i=0, l=obj.length; i<l; i++) {
fn.call(null, obj[i], i, obj);
}
}
// Iterate over object keys
else {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
fn.call(null, obj[key], key, obj);
}
}
}
};