Open Rain120 opened 2 years ago
function getValue(source, path, defaultValue = null) {
const paths = path.replace(/\[(\d+)\]/g, '.$1').split('.');
let result = source;
for (let key of paths) {
// null 与 undefined 取属性会报错, 用Object包装一下
result = Object(result)[key];
}
return result || defaultValue;
}
function get(obj, ...args) {
return args.map(key => getValue(obj, key));
}
题目