function getPropByPath(obj, path) {
let tempObj = obj;
path = path.replace(/[(w+)]/g, '.$1');
path = path.replace(/^./, '');
let keyArr = path.split('.');
let i = 0;
for (let len = keyArr.length; i < len - 1; ++i) {
if (!tempObj) break;
let key = keyArr[i];
if (key in tempObj) {
tempObj = tempObj[key];
} else {
return null
//break;
}
}
return tempObj ? tempObj[keyArr[i]] : null
}
getPropByPath(obj, 'a.b.c') // 1
getPropByPath(obj, 'a[b][c]') // 1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…