antd-mini
Version:
antd-mini 是支付宝小程序 UI 组件库,遵循 Ant Design 规范。
11 lines (10 loc) • 476 B
JavaScript
export function get(obj, path, defaultValue) {
// If path is not an array, convert it to an array
if (!Array.isArray(path)) {
path = path.split('.').map(function (key) { return key.replace(/\[(\d+)]/g, '$1'); });
}
// Use reduce to traverse the path
var result = path.reduce(function (acc, key) { return (acc && acc[key] !== undefined ? acc[key] : undefined); }, obj);
return result === undefined ? defaultValue : result;
}
export default get;