import isArrayLike from './isArrayLike'; import callFn from './callFn'; /** * 遍历集合 * @param {Array|object} collection 集合 * @param {Function} cb 回调方法 */ export default function each (collection: Array|object, cb: Function): void { if (!collection) { return; } let _isArray: boolean = isArrayLike(collection); if (_isArray) { for (let i: number = 0; i < (>collection).length; i++) { let _item = collection[i]; let _res = callFn(cb, [_item, i, collection]); if (_res === false) { break; } } } else { for (let key in collection) { if (collection.hasOwnProperty(key)) { let _item = collection[key]; let _res = callFn(cb, [_item, key, collection]); if (_res === false) { break; } } } } }