3 回答
![?](http://img1.sycdn.imooc.com/5333a0780001a6e702200220-100-100.jpg)
TA贡献1829条经验 获得超13个赞
是的。你可以在数组上使用一个reduce:
let result = array.reduce((value, entry) => value[entry], obj);
![?](http://img1.sycdn.imooc.com/5458626a0001503602200220-100-100.jpg)
TA贡献1836条经验 获得超4个赞
let desired = obj;
while(array.length > 0) { desired = desired[array[0]]; array.shift() }
console.log(desired)
这应该有效
![?](http://img1.sycdn.imooc.com/54586870000183e302200220-100-100.jpg)
TA贡献1862条经验 获得超7个赞
这是一种方法:
let obj = [{
"testKeyOne": "one",
"testKeyTwo": "two"
},
{
"testKeyThree": "three",
"testKeyFour": "four",
"testKeyFive": {
"testKeyFiveAndAHalf": "5.5",
"testKeyFiveAndThreeQuarters": "5.75"
}
},
]
let arr = [
[1, "testKeyFive", "testKeyFiveAndAHalf"],
[0, "testKeyOne"]
]
function foo(objArr, accessArr) {
for (const [index, ...keys] of accessArr) {
let obj = objArr[index];
for (const key of keys) {
obj = obj[key];
}
console.log(obj)
}
}
foo(obj, arr);
添加回答
举报