【发布时间】:2020-06-09 12:39:21
【问题描述】:
我们以这个对象为例:
var obj = {a: {b: {c: 'result'}}}
我知道我可以得到c的值,这样做:
console.log(obj.a.b.c) // 'result'
或者这个:
console.log(obj['a']['b']['c'])
但是如何在函数中将 obj 和列作为参数传递给 c 的值?
function func(obj, attributes) {
return obj[attributes]
}
console.log(func(obj, a.b.c)) // how to make this work
console.log(func(obj, ['a']['b']['c'])) // or this
【问题讨论】:
-
你知道 lodash 库吗?
标签: javascript