虽说javascript没有二维数组概念,但以下这个程序为什么会错误?
function getQueryStringArgs() {
var qs = (location.search.length > 0) ? location.search : '';
qs = qs.substr(1);
var array = new Array();
qs = qs.split('&');
for (var i = 0; i < qs.length; i++) {
var arr = qs[i].split('=');
array[i]['name'] = arr[0];
array[i]['value'] = arr[1];
}
alert(array);
}
var param = getQueryStringArgs();
错误消息:TypeError: array[i] is undefined
但http://blog.csdn.net/lincnl/article/details/2503118这个博文提到可以这样的喔……
------解决方案--------------------function getQueryStringArgs() {
var qs = (location.search.length > 0) ? location.search : '';
qs = qs.substr(1);
var array = new Array();
qs = qs.split('&');
for ( var i = 0; i < qs.length; i++) {
var array[i] = new Array();
var arr = qs[i].split('=');
array[i]['name'] = arr[0];
array[i]['value'] = arr[1];
}
//alert(array);
}
var param = getQueryStringArgs();
------解决方案--------------------给array[i]先赋值就可以了:
function getQueryStringArgs() {
var qs = (location.search.length > 0) ? location.search : '';
qs = qs.substr(1);
var array = new Array();
qs = qs.split('&');
for (var i = 0; i < qs.length; i++) {
var arr = qs[i].split('=');
array[i] = {};
array[i]['name'] = arr[0];
array[i]['value'] = arr[1];
}
alert(array);
}
var param = getQueryStringArgs();
------解决方案--------------------function Get2DArray2()
{
var arr = new Array();
arr[0] = new Array();
arr[1] = new Array();
arr[0][0] = "00";
arr[0][1]="01";
arr[1][0]="10";
arr[1][1]="11";
for(i=0; i<arr.length;i++)
{
for(j=0; j<arr[i].length;j++)
{
&nbs