UNPKG

flo-utils

Version:
36 lines (29 loc) 773 B
/** * @name getValueByPath * @param {any} entity * @param {any} path a.b.c.d * @param {string} [defaultValue=""] * @returns * @description 通过路径获取数据 */ function getValueByPath(entity, path) { var defaultValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '-'; var props = path; if (typeof props === 'string') { props = props.split('.').filter(function (d) { return d; }); } var current = entity; for (var i = 0; i < props.length; i += 1) { if (current === null || current === undefined) { return defaultValue; } current = current[props[i]]; } if (current === null || current === undefined) { return defaultValue; } return current; } export default getValueByPath;