我的数据是:
var x = [
{
"data":{
"plmn":"Tel",
"id":"B193",
"cell":{
"rsrp":[
-122.81,
-118,
-118
],
"rsrq":[
-19.81,
-14.44,
-14.44
],
"earfcn":9410,
"pci":246,
"celltiming":[
230494,
230494
],
"sinr":[
-9.4,
-2
]
},
"mac":"9C65F9210F5B"
},
"time":1499188642310
},
{
"data":{
"plmn":"Tel",
"id":"B193",
"cells":{
"rscp":[
-12.1,
-108,
-108
],
"rsri":[
-10.41,
-13.24,
-13.24
],
"earfcn":9411,
"pci":245,
"celltiming":[
230494,
230494
],
"sinr":[
-9.2,
-2
]
},
"mac":"9C65F9210F5C"
},
"time":1499188642310
}
]
我的下拉列表包含选项cell
和cells
。因此,当我选择一个选项(例如:cell
)时,我运行以下脚本:
var variable = $('#dropdown option:selected').text(); // it mean = 'cell'
x.forEach(function(d){
d.data.variable.rsrq.forEach(function(d,i){
console.log('length of array rsrq is '+ i)
}) // and error here with message Cannot read property 'rsrq' of undefined
即使对变量进行硬编码,例如:var myvar = 'cell'
也无法正常工作。