Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare empty array in javascript

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


}
like image 477
user3944364 Avatar asked Mar 25 '15 11:03

user3944364


People also ask

Can we declare empty array in JavaScript?

There are various methods to empty an array in JavaScript. Assigning the array to an empty array is the quickest method of emptying an array in JavaScript. In javascript, length is a property that, when set to 0, clears the array. splice() method can be used to delete 1 or more elements from the array in JavaScript.

How do you declare an empty array of objects in JavaScript?

To initialize an array of objects, use the Array() constructor to create an array filled with N empty elements and use a for loop to iterate over the array assigning each element to an object. Copied! const arr2 = new Array(2); for (let i = 0; i < arr2.

Can you create an empty array?

It is possible to create an empty array and fill it by growing it dynamically. That's not a very efficient technique, though. Prefer to preallocate the array and fill it in so it doesn't have to grow with each new element you add to it.


3 Answers

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
});
like image 169
R3tep Avatar answered Oct 12 '22 23:10

R3tep


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
like image 44
fedmich Avatar answered Oct 13 '22 00:10

fedmich


The following code used to declare empty array in javascript

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

console.log(product_arr);
like image 32
Vijay Avatar answered Oct 12 '22 22:10

Vijay