最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Declare empty array in javascript - Stack Overflow

programmeradmin1浏览0评论

update

My code that works. When page is loaded

    product= [[],[]]; 

then the code executed after ajax call:

$('#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
});

To use eval(); in ajax response for this, with some changes in php file? /endupdate

product[0]=[1,2,3,4];

product[1]=[a,b,x,z];

.

.

product[10]=[extra,extra,extra,extra];

When I load the page this is executed: product= [[],[],[],[],[],[],[],[],[],[]];

But if I declare this, when I call ajax I can push add data only to this array (10 rows) If I have 11 rows (product[10][0] and product[10][1]), the extra data will not be added. After ajax call I need the extra data : product= [[],[],[],[],[],[],[],[],[],[],**[11]**];

This function is because I want to put data in array after loading ajax data from php file.

$('#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
});


}

update

My code that works. When page is loaded

    product= [[],[]]; 

then the code executed after ajax call:

$('#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
});

To use eval(); in ajax response for this, with some changes in php file? /endupdate

product[0]=[1,2,3,4];

product[1]=[a,b,x,z];

.

.

product[10]=[extra,extra,extra,extra];

When I load the page this is executed: product= [[],[],[],[],[],[],[],[],[],[]];

But if I declare this, when I call ajax I can push add data only to this array (10 rows) If I have 11 rows (product[10][0] and product[10][1]), the extra data will not be added. After ajax call I need the extra data : product= [[],[],[],[],[],[],[],[],[],[],**[11]**];

This function is because I want to put data in array after loading ajax data from php file.

$('#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
});


}
Share Improve this question edited Mar 25, 2015 at 15:31 user3944364 asked Mar 25, 2015 at 11:20 user3944364user3944364 1211 gold badge3 silver badges11 bronze badges 3
  • 1 Why pre-allocate/dimension? Create a basic empty array =[]; then .push to it in the loop as needed – Alex K. Commented Mar 25, 2015 at 11:26
  • I change the data after ajax call. First time I have 2 rows with data. After the call there can be 10 rows. – user3944364 Commented Mar 25, 2015 at 11:44
  • @user3944364 Post the code of your ajax call – R3tep Commented Mar 25, 2015 at 11:54
Add a comment  | 

4 Answers 4

Reset to default 6

In the succes of your ajax call use the function push()

product.push([]);

This adds an array at the last index of product. Like that ,the index 10 is created and you have an extra data.

If you want to add a dynamic number of rows, use this code :

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

Another way

In your ajax return save the new length of your array product in a global variable. And use it before your loop to reset your array and initialize it with the new length.

var lengthArray = 10; // update the value in the callback of your ajax call

And your loop :

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
});

Note: this line of your code produces a string, not an array.

var product = $(ul).html();  //returns string not an array

what you need is something like

var product_arr = {}; // an object or 
var product_arr = []; // an array

The following code used to declare empty array in javascript

var product_arr = new Array(); //declaring empty array

console.log(product_arr);

var product = [];

was the answer I was looking for personally

发布评论

评论列表(0)

  1. 暂无评论