loadash里有很多非常好用的封装好的库函数,比如get,深拷贝等等。
我们就来看看get函数是怎么实现的。
get
//lodash的get方法
function get(object, path, defaultValue = 'undefined') {
//构造数组,将'['和']'替换成'.'
let newPath = [];
newPath = path.replace(/\[/g, '.').replace(/\]/g, '').split('.');
return newPath.reduce((a, b) => {
return (a || {})[b]
}, object) || defaultValue
}
//测试
const obj = {
a: {
b: [{
c: 1
}]
}
}
console.log(get(obj, 'a.b[0].c[1].e[2][1].e', 0));
console.log(get(obj, 'a.b[0].c', 0));
console.log(get(obj, 'a.b.c', 0));
console.log(get(obj, 'a', 0));
首先构造数组,把中括号替换掉。再reduce循环即可。
记录记录!