How to use foreach in a multidimensional array using jQuery? Strange behavior

Just if someone can explain to me why the alert box does not return an array but is empty

var response = new Array();
response[0] = new Array();
response[1] = new Array(); 
response[2] = new Array();  

response[0]["Id"] = 1;
response[0]["StreetAddress"] = 'xxx';
response[0]["Place"] = 'yyy';

response[1]["Id"] = 2;
response[1]["StreetAddress"] = 'xxx';
response[1]["Place"] = 'yyy';

response[2]["Id"] = 3;
response[2]["StreetAddress"] = 'xxx';
response[2]["Place"] = 'yyy';

$.each(response , function(key1, value1) {
    alert(value1);
});

In fact, I will have such an array from the web service, and I need to loop around on this array to get the data.

But I do not understand why the loop is not working properly.

Thank you guys in advance.

+5
source share
3 answers

This is not a multidimensional array, but invalid code. Arraysand Objects(hashes) are two different things, not php.

So, at the top you should write the following:

var response = new Array();
response[0] = new Object();
response[1] = {}; // it the same
response[2] = new Object();

And you can iterate over it just like you:

$.each(response , function( index, obj ) {
    $.each(obj, function( key, value ) {
        console.log(key);
        console.log(value);
    });
});
+8

: console.log(response)... , , , .

JSON?

var response = [{
    "Id":"1",
    "StreetAddress": "xxx",
    "Place":"yyy"
},
{
    "Id":"2",
    "StreetAddress": "xxx2",
    "Place":"yyy2"
},
{
    "Id":"3",
    "StreetAddress": "xxx3",
    "Place":"yyy3"
}
]
console.log(response);
//you'll get an object: [Object { Id="1", StreetAddress="xxx", Place="yyy"}, Object { Id="2", StreetAddress="xxx2", Place="yyy2"}, Object { Id="3", StreetAddress="xxx3", Place="yyy3"}]
//iterate over
for(var x=0; x < response.length; x++){
    console.log("ID: " + response[x].Id + " StreetAddress: " + response[x].StreetAddress + " Place: " + response[x].Place);
}
+5

You should not use such arrays in Javascript. Arrays are numerically indexed. If you write

response[1]["Id"] = 2; 

you add a property to the response array [1]

EDIT - I read a little better than your comment. It states:

// FYI: the output is an array of keys (for example, the response [0] .Id), keys:

So you have an array of objects.

This displays the data you receive.

var response = new Array;
response[0] = new Object();
response[1] = new Object(); 
response[2] = new Object();  

response[0]["Id"] = 1;
response[0]["StreetAddress"] = 'xxx';
response[0]["Place"] = 'yyy';

response[1]["Id"] = 2;
response[1]["StreetAddress"] = 'xxx';
response[1]["Place"] = 'yyy';

response[2]["Id"] = 3;
response[2]["StreetAddress"] = 'xxx';
response[2]["Place"] = 'yyy';

and you can access them as follows:

jQuery.each(response, function(key, value){
         for (key2 in value[key]){
            if (value[key].hasOwnProperty(key2)){
            alert(mine[key2])
            }
         }
     });
+1
source

All Articles