-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-listArr.js
72 lines (56 loc) · 1.85 KB
/
06-listArr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function typeOf(obj) {
return Object.prototype.toString.call(obj).replace(/\[object\s|]/g, '');
}
function isArrayLike(obj) {
const length = !!obj && 'length' in obj && obj.length; //判断是否为数组或数组对象,获取长度
const type = typeOf(obj); // 获取类型
return (
type === 'Array' ||
length === 0 ||
(typeof length === 'number' && length > 0 && length - 1 in obj)
);
}
/**
* 遍历对象或数组,或者类数组,例如React中的children对象、arguments等
* @param {Object|Array} obj
* @param {Function} callback fn(n, i) or fn(val, key)
* @param {Number} [direction = 1] 是否倒序遍历,只对数组有效
* @return {Object|Array}
*
* @example
* // 遍历数组
* object.each([100, 200, 300], (n, i) => console.log(n, i));
* // 遍历json对象
* object.each({a: 100, b: 200}, (value, key) => console.log(key, value));
* // 遍历React子节点
* object.each(this.props.children, (child, index) => console.log(child));
* // 遍历arguments
* object.each(arguments, (arg, i) => console.log(arg));
*/
function each(obj, callback, direction=1) {
console.log(direction)
const reversed = true;
const length = obj.length;
let value,
i = reversed ? length - 1 : 0;
if (isArrayLike(obj)) {
for (; i < length && i >= 0; reversed ? i-- : i++) {
value = callback.call(obj[i], obj[i], i);
if (value === false) {
break;
}
}
} else {
for (i in obj) {
/* istanbul ignore else */
if (obj.hasOwnProperty(i)) {
value = callback.call(obj[i], obj[i], i);
if (value === false) {
break;
}
}
}
}
return obj;
}
each([1,2,3], (value, key) => console.log(key, value) );