Cheitan Cheitan - 2 years ago 64
Javascript Question

Object browsing and "array" properties

I got a problem with one of my application today. With Ajax I get an object which is perfectly filled with the wanted values (checked with

). In this object each property is an array containing several numbers.

In my app I'm trying to browse through this object in order to get each array and do some stuff with it. The problem here is that I can't get back the array. In fact when using
for(var p in myObject)
it only get the property's name and not the value linked to it.

Here is a very tiny jsFiddle made to expose what I want to do :
When using this code the console is logging plenty of string values but no array.

I think this have a link with the loop, which I guess loop on properties names, but how can I obtain what I want ?

Answer Source

To get the values of the key you need to use it like myObject[data]

Also note the : instead of = in object

var myObject = {
        first : [0,13,41,9],
      second : [33,6],
      third : [1,1,23]

for(var data in myObject){
    console.log("data is "+data+" value is " +myObject[data]);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download