4

I have a two dimensional JSON array where each element contains several attributes. The example below is intentionally simplified:

var map_data = { "1":
                      {"1":{"name":"aa"},"2":{"name":"bb"}},
                 "2":
                      {"1":{"name":"cc"},"2":{"name":"dd"}}
               };

I try to parse the data but .length doesn't work:

for(x=1; x<=map_data.length; x++) { 
    for(y=1; y<=map_data[x].length; y++) {
        // CODE 
    }
}

Many, many thanks!

5 Answers 5

7

That's not an array, they are simple objects, that's why you cannot use the length property.

You need to use the for...in statement:

for(var x in map_data) { 
  if (map_data.hasOwnProperty(x))
    for(var y in map_data[x]) {
      if (map_data[x].hasOwnProperty(y)) {
        // CODE
      }
    }
}

The hasOwnProperty checks are because this statement iterates over all properties, inherited or not, and if something (like some JavaScript frameworks) augmented the Array.prototype or Object.prototype objects, those augmented properties will be also iterated.

You should know that this statement doesn't ensure anyhow the order of iteration.

I would recommend you to use a "real" array:

[
 [{"name":"aa"},{"name":"bb"}],
 [{"name":"cc"},{"name":"dd"}]
]

In this way you will be able to use the length property to iterate over the indexes.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for pointing in the direction of a real array. It never ceases to amaze me how many questions involve setting numerical index properties on objects.
3

hasOwnProperty is used to determine whether an object has the specified property as a direct property of that object with no respect to it's prototype chain.

for(var i in map_data){
    if(map_data.hasOwnProperty(i)){
        for(var j in map_data[i]){
            if(map_data[i].hasOwnProperty(j)){
                /*do whatever you want with map_data[i][j]*/
            }
        }
    }
}

Comments

2

Since map_data is an object instead of an array you need to use for/in loop:

for (var x in map_data) {
  // check for hasOwnProperty omitted for simplicity.
  for (var y in map_data[x]) {
    // CODE.
  }
}

But it's better to send that JSON as an array [a,b,c] instead of an object {"0":a,"1":b,"2":c}.

Comments

0

Use the for .. in construct:

for (var m in map_data) {
     // ...
}

Also, I must note that this isn't a "JSON array", but rather a nested object. JSON is just the string representation of nested JS objects and arrays.

Comments

0

if it helps someone, its a example of c# and javascript:

c#:

List<List<string>>  list_array = new List<List<string>>();

JavaScriptSerializer jss = new JavaScriptSerializer();
string _myJSONstring = jss.Serialize(list_array); 

/*in this case list_array is a list bidimensional, but can be a single array, if you print _myJSONstring, this will show like this: "[["XX","AAAA"],["YY","BBBB"]]" */

javascript:

into a function that get the string from c#:

var a = JSON.parse(array);
    for (var t = 0; t < array.length; t++) {
        for (v = 0; v < array[t].length; v++) {
            alert(array[t][v]);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.