var obj = {
selector: {
to: { toutiao: "FE Coder"}
},
target: [
1,
2,
{ name: 'byted' }
]
};
function get(data, ...args) {
const res = JSON.stringify(data);
var a = args.map((item) => (new Function(`return ${res}.${item} `))());
// var a = args.map((item) => (eval(`(function(){ return ${res}.${item} })`))());
console.log(a)
}
get(obj, 'selector.to.toutiao', 'target[0]', 'target[2].name');
输入任意对象,任意参数路径能获取对应的值的 js 函数
猜你喜欢
转载自blog.csdn.net/BetterGG/article/details/84853696
今日推荐
周排行