Creating And Iterating ArrayList In jQuery

In this blog, we will learn how to create an ArrayList in jQuery and how to iterate through it and read its items.

In jQuery, there are multiple parameters in a list. We can add these parameters in a jQuery ArrayList. For that, first we have to make an array list object and then the value will be pushed in this list.

 

Array in jQuery

 
Declare Array
  1. var DataList=[];  
The above code has created an empty array.
 
Assigning valus at the time of declaration in Array
  1. var fruits = ["Banana""Orange""Apple""Mango"];  
  2. var number=["1","2","3","4"];
The above code has created an array with assigned values.
 

ArrayList in jQuery

 
ArrayList is used when we want to add multiple values of multiple types in an array. 
  1. DataList.push({  
  2.    Id:1,  
  3.    Name: ABC  
  4. });  
  5. DataList.push({
  6.    Id:2,
  7.    Name:ABC
  8. });

The above code has created "DataList" as an ArrayList with multiple parameters.
 
Explanation
 
Id=1, name="ABC" has 0 Index.
Id =2 ,name="ABC" has 1 Index 
 
Assigning values at the time of declaration in ArrayList
  1. DataList=[{      
  2.    Id:1,      
  3.    Name:ABC      
  4. }    
  5. {Id:2,    
  6.    Name:XYZ    
  7. }];    
Explanation
 
Id=1,Name="ABC" is in 0 index
Id=2,Name="XYZ" is in 1 index
 
Assigning values in ArrayList using for Loop
 
The loop starts from 0 index and goes up to the length of the DataList. We are assigning the values of the Ids to Id parameter and naming to the naming parameter. 
  1. var Ids=1;    
  2. var naming="ABC";    
  3. for(var i=0;i<DataList.length;i++)    
  4. {    
  5.     Ids=DataList[i].Id;    
  6.    naming=DataList[i].Name;    
  7. }   
While accessing DataList, we will use the following.
 
DataList[i].Id
i is the Index of the DataList.
DataList[0].Id=1
DataList[0].Name=ABC
 
Output
 
1
ABC