functioneach(obj, callback) { let length, i = 0; // 判断是否为类数组对象 if (isArrayLike(obj)) { length = obj.length; for (; i < length; i++) { // 如果回调函数放回false,中断迭代器 if (callback.call(obj[i], i, obj[i]) === false) { break; } } } else { for (i in obj) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } }
return obj }
let data = { 0: "a", 1: "b", length: 2 } each(data, (key, val) => { console.log(key, val); // 0 a // 1 b });