使用变量访问未定义的数组结果



我从GET响应返回了几个数组,我需要访问这些数组。由于它们的名称可以根据请求而更改,因此我有一个变量来指定要使用的数组。

然而,我总是被定义不明确。看到这个:

            console.log(current); // trips_out_201702
            console.log(theResponse.current); // undefined
            console.log(theResponse.trips_out_201702); // this works

如何使theResponse.current工作,使其返回current实际代表的内容?为什么我在那里未定义?

当对象中的属性键是变量时,可以使用方括号表示法访问该属性。

  console.log(theResponse[current]);
当使用

动态属性时 你应该这样做

theResponse[current] not theResponse.current

您正在尝试使用对象的方式获取数组值。你应该立即尝试一下variable['keyName']祝你好运!

最新更新