Just if someone can explain me why the alertbox doesn't return an array but 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);
});
Actually, I will have this kind of array from a webservice and I need to loop into this array to retrieve datas.
实际上,我将从web服务中获得这种数组,我需要循环到这个数组中来检索数据。
But I don't understand why the loop doesn't work properly.
但我不明白为什么循环不能正常工作。
Thanks you in advance guys.
先谢谢你们。
3 个解决方案
#1
8
That's not a multidimensional array, but an invalid code. Arrays
and Objects
(Hashes) are different things not as php does.
这不是一个多维数组,而是一个无效的代码。数组和对象(哈希)是不同的东西,不像PHP那样。
So at the top you should write the following:
所以在顶部你应该写下面的内容:
var response = new Array();
response[0] = new Object();
response[1] = {}; // it's the same
response[2] = new Object();
And you could iterate over it as you did:
你可以像你一样迭代它:
$.each(response , function( index, obj ) {
$.each(obj, function( key, value ) {
console.log(key);
console.log(value);
});
});
#2
5
if you try: console.log(response) ... you'll see the array is empty, it seems the array is not well formed.
如果你尝试:console.log(响应)...你会看到数组是空的,似乎数组不是很好。
why don't you use JSON format instead?
为什么不使用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);
}
#3
1
You should not use arrays like this in Javascript. Arrays are numerically indexed. If you write
你不应该在Javascript中使用这样的数组。数组被数字索引。如果你写
response[1]["Id"] = 2;
you are adding a property to response[1] array
你正在为response [1]数组添加一个属性
EDIT - i've read a little better your coment. It states:
编辑 - 我读了你的co更好一点。它指出:
//FYI: The output is an array of key value pairs (e.g. response[0].Id), the keys being:
// FYI:输出是键值对的数组(例如response [0] .Id),键是:
So you have an array of objects.
所以你有一个对象数组。
This maps the data you will 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 like this:
你可以像这样访问它们:
jQuery.each(response, function(key, value){
for (key2 in value[key]){
if (value[key].hasOwnProperty(key2)){
alert(mine[key2])
}
}
});
#1
8
That's not a multidimensional array, but an invalid code. Arrays
and Objects
(Hashes) are different things not as php does.
这不是一个多维数组,而是一个无效的代码。数组和对象(哈希)是不同的东西,不像PHP那样。
So at the top you should write the following:
所以在顶部你应该写下面的内容:
var response = new Array();
response[0] = new Object();
response[1] = {}; // it's the same
response[2] = new Object();
And you could iterate over it as you did:
你可以像你一样迭代它:
$.each(response , function( index, obj ) {
$.each(obj, function( key, value ) {
console.log(key);
console.log(value);
});
});
#2
5
if you try: console.log(response) ... you'll see the array is empty, it seems the array is not well formed.
如果你尝试:console.log(响应)...你会看到数组是空的,似乎数组不是很好。
why don't you use JSON format instead?
为什么不使用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);
}
#3
1
You should not use arrays like this in Javascript. Arrays are numerically indexed. If you write
你不应该在Javascript中使用这样的数组。数组被数字索引。如果你写
response[1]["Id"] = 2;
you are adding a property to response[1] array
你正在为response [1]数组添加一个属性
EDIT - i've read a little better your coment. It states:
编辑 - 我读了你的co更好一点。它指出:
//FYI: The output is an array of key value pairs (e.g. response[0].Id), the keys being:
// FYI:输出是键值对的数组(例如response [0] .Id),键是:
So you have an array of objects.
所以你有一个对象数组。
This maps the data you will 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 like this:
你可以像这样访问它们:
jQuery.each(response, function(key, value){
for (key2 in value[key]){
if (value[key].hasOwnProperty(key2)){
alert(mine[key2])
}
}
});