Wednesday, May 8, 2019

javascript - How to create dynamic two dimensional array in jquery or js




I need to create global two dimensional array in jquery or javascript



My function is like this









On click of that button I am getting this error "globalArray[0] is undefined"



How can I create global dynamic multi dimensional array.


Answer



if (!globalArray[index]) 
globalArray[index] = []; // init the array.

globalArray[index].push(name);



You have a typo with the dot:



$.("#uname").val(); 


Change to:



$("#uname").val();



What are you trying to do with this code?






Update: (The question was totally edited.)



Your code:



var globalArray[0] = new Array(); 



globalArray[0] is invalid variable name, you need first to declare the array:



var globalArray = []; // Array literal.
globalArray[0] = [] // The element at position 0 is new an array.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...