map, reduce和正则
const obj = {
selector: {
to: { toutiao: "FE Coder"}
},
target: [1, 2, { name: 'byted'}]
};
get(obj, 'selector.to.toutiao', 'target[0]', 'target[2].name');
// [ 'FE Coder', 1, 'byted']有缺陷的第一版
function get(data, ...args) {
return args.map((item) => {
const paths = item.split('.');
let res = data;
paths.map(path => res = res[path]);
return res;
})
}改进后的解法
最后更新于