UNPKG

vtils

Version:

一个面向业务的 JavaScript/TypeScript 实用程序库。

39 lines (38 loc) 881 B
"use strict"; exports.__esModule = true; exports.traverse = traverse; var _lodashUni = require("lodash-uni"); /** * 遍历对象和数组。 * * @param value 要遍历的值 * @param callback 遍历回调 * @returns 返回结果 * @example * ```typescript * traverse([1, 2, {3: 4}], value => { * console.log(value) * // => 1 * // => 2 * // => {3: 4} * // => 4 * }) * ``` */ 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 ((0, _lodashUni.isPlainObject)(value)) { (0, _lodashUni.forOwn)(value, function (item, key) { callback(item, key, value); if ((0, _lodashUni.has)(value, key)) { traverse(item, callback); } }); } }