vtils
Version:
一个面向业务的 JavaScript/TypeScript 实用程序库。
36 lines (35 loc) • 787 B
JavaScript
import { forOwn, has, isPlainObject } from 'lodash-uni';
/**
* 遍历对象和数组。
*
* @param value 要遍历的值
* @param callback 遍历回调
* @returns 返回结果
* @example
* ```typescript
* traverse([1, 2, {3: 4}], value => {
* console.log(value)
* // => 1
* // => 2
* // => {3: 4}
* // => 4
* })
* ```
*/
export function traverse(value, callback) {
if (Array.isArray(value)) {
value.forEach(function (item, index) {
callback(item, index, value);
if (value[index] !== undefined) {
traverse(item, callback);
}
});
} else if (isPlainObject(value)) {
forOwn(value, function (item, key) {
callback(item, key, value);
if (has(value, key)) {
traverse(item, callback);
}
});
}
}