const obj = {
a: {
b: [1, 2, 3]
}
}
function get(target, str) {
const path = str.replace(/\[/g, '.').replace(/\]/, '.').split('.')
return path.reduce((pre, cur) => {
return pre[cur]
}, target)
}
console.log(get(obj, 'a.b[2]')); // 3
reduce实现lodash中的get方法
最新推荐文章于 2024-06-10 22:18:41 发布