antd-mini
Version:
antd-mini 是支付宝小程序 UI 组件库,遵循 Ant Design 规范。
17 lines (13 loc) • 440 B
text/typescript
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((key) => key.replace(/\[(\d+)]/g, '$1'));
}
// Use reduce to traverse the path
let result = path.reduce(
(acc, key) => (acc && acc[key] !== undefined ? acc[key] : undefined),
obj
);
return result === undefined ? defaultValue : result;
}
export default get;