在javascript中声明空数组

use*_*364 6 javascript arrays ajax jquery eval

更新

我的代码有效.加载页面时

    product= [[],[]]; 
Run Code Online (Sandbox Code Playgroud)

那么在ajax调用之后执行的代码:

$('#contextreload ul').each(function(i, ul) {
product.push([]);
});

$('#contextreload ul').each(function(i, ul) {
  allline=i; 
  $('#reloadajax'+i+' li').each(function(lk, li) {
  var lilk = $(li).html();  product[i][lk]=lilk;

  // your code goes here
 });

  // your code goes here
});
Run Code Online (Sandbox Code Playgroud)

使用eval(); 在ajax响应中,在php文件中有一些变化? / endupdate

产物[0] = [1,2,3,4];

产物[1] = [A,B,X,Z];

.

.

产物[10] = [额外,额外,额外,额外];

当我加载页面时执行: product= [[],[],[],[],[],[],[],[],[],[]];

但是,如果我声明这一点,当我打电话AJAX我可以数据仅添加到这个数组(10行),如果我有11行(product[10][0]product[10][1]),额外的数据不会被添加.在ajax调用之后我需要额外的数据:product= [[],[],[],[],[],[],[],[],[],[],**[11]**];

这个函数是因为我想在从php文件加载ajax数据后将数据放入数组中.

$('#contextreload ul').each(function(i, ul) {
 <strike> var product = $(ul).html();  </strike>
    allline = i; 

    $('#reloadajax'+i+' li').each(function(lk, li) {
        var lilk = $(li).html();  
        product[i][lk]=lilk;
        alert(lilk+lk);
        // your code goes here
    });
    // your code goes here
});


}
Run Code Online (Sandbox Code Playgroud)

R3t*_*tep 6

在你的ajax调用的成功中使用该函数 push()

product.push([]);
Run Code Online (Sandbox Code Playgroud)

这会在最后一个索引处添加一个数组product.像这样,10创建索引并且您有一个额外的数据.

如果要添加动态行数,请使用以下代码:

var number_of_new_row = 11; // 11 for example, you can use any number >= 0
for(; number_of_new_row--;)
    product.push([]);
Run Code Online (Sandbox Code Playgroud)

其他方式

在ajax返回中,将数组的新长度保存product在全局变量中.并在循环之前使用它来重置数组并使用新长度初始化它.

var lengthArray = 10; // update the value in the callback of your ajax call
Run Code Online (Sandbox Code Playgroud)

你的循环:

var product = [];
for(; lengthArray--;)
    product.push([]);

$('#contextreload ul').each(function(i, ul) {
    //<strike> var product = $(ul).html();  </strike>
    allline = i; 

    $('#reloadajax'+i+' li').each(function(lk, li) {
        var lilk = $(li).html();  
        product[i][lk]=lilk;
        alert(lilk+lk);
        // your code goes here
    });
    // your code goes here
});
Run Code Online (Sandbox Code Playgroud)