0

My data is :

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
   }
] 

My dropdown list contains options cell and cells. So, when i choose an option (ex: cell), i run the following script :

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

Even hardcoding the variable, for ex: var myvar = 'cell' doesn't work.

Parag
  • 400
  • 1
  • 3
  • 11
Onyot
  • 13
  • 4
  • `variable` here is a variable holding property name. It should be `d.data[variable].rsrq`. `data.variable` will look for a property whose name is `variable` and not process `variable` – Rajesh Sep 13 '17 at 06:41
  • [Related](https://stackoverflow.com/a/11922384/1169519). – Teemu Sep 13 '17 at 06:47
  • okay thanks and working fine now – Onyot Sep 13 '17 at 08:46

0 Answers0