You can have multiple option to do loop, see below examples
var arrayCollection = ["ABC", "DEF", "HIJ"];$.each(arrayCollection, function(index, value){ console.log("index : ",index, " value :", value);});$(arrayCollection).each(function(index, value){ console.log("index : ",index, " value :", value);});for(var i = 0; i <; arrayCollection.length; i++) { console.log("index : ", i , " value :", arrayCollection[i]);} $.map(arrayCollection, function( val, i ) { console.log("index : ", i , " value :", val);});arrayCollection.map((val, i ) =>; console.log("index : ", i , " value :", val));
var arrayCollection = ["ABC", "DEF", "HIJ"];
$.each(arrayCollection, function(index, value){
console.log("index : ",index, " value :", value);
});
$(arrayCollection).each(function(index, value){
for(var i = 0; i <; arrayCollection.length; i++) {
console.log("index : ", i , " value :", arrayCollection[i]);
}
$.map(arrayCollection, function( val, i ) {
console.log("index : ", i , " value :", val);
arrayCollection.map((val, i ) =>; console.log("index : ", i , " value :", val));
var Array = ["A", "B", "C", "D", "E"]; $.each(Array, function(index, value){ $("#result").append(index + ": " + value + '<;br>;'); });`
var Array = ["A", "B", "C", "D", "E"];
$.each(Array, function(index, value){
$("#result").append(index + ": " + value + '<;br>;');
});`
You can achieve this in the following ways
1.jQuery.each()eg. var numbers = [1, 2, 3, 4];$.each(numbers,function(index, value){console.log(“Index- “, index, “Value- “, value);});
2.for loop
eg. var numbers = [1, 2, 3, 4];for(var i = 0; i < numbers.length; i++){console.log(“loop”, numbers[i]);}
3.for ineg. var numbers = [1, 2, 3, 4];for(var i in numbers){console.log(“loop”, numbers[i]);}
4.for ofeg. var numbers = [1, 2, 3, 4];for(var i of numbers){console.log(“loop”, numbers[i]);}
5.forEacheg. var numbers = [1, 2, 3, 4];numbers.forEach(function(value, index, numbers){console.log(“Index- “, index, “Value- “, value);});
6.$.grep()eg. var numbers = [1, 2, 3, 4];$.grep(numbers,function(value, index){console.log(“Index- “, index, “Value- “, value);});
One way it's by using a for each iterator or using the foreach method that is part of every array arrange, like :arrayList.forEach(item, value){}
var array = [1, 2, 3, 4];
//loop from 0 index to max indexfor(var i = 0; i < array.length; i++) { console.log(array[i])}
var substr = [1, 2, 3, 4];$.each(substr , function(index, val) { console.log(index, val)});
var myObj = { firstName: “skyfoot”};$.each(myObj, function(propName, propVal) { console.log(propName, propVal);});
$.each([ 52, 97 ], function( index, value ) { alert( index + ": " + value );});
$.each([ 52, 97 ], function( index, value ) {
alert( index + ": " + value );
You can use a for each loop in jQuery to loop through an arraylist. The following code snippet creates a list of objects with 2 items, Id and Name and iterates through the arraylist items.
var Ids=1; var naming="ABC"; var DataList=[{ "Id":1, "Name":"ABC" }, {"Id":2, "Name":"XYZ"}]; $.each( DataList, function( key, value ) { Ids =value.Id; naming =value.Name; });
var Ids=1;
var naming="ABC";
var DataList=[{ "Id":1, "Name":"ABC" }, {"Id":2, "Name":"XYZ"}];
$.each( DataList, function( key, value ) {
Ids =value.Id;
naming =value.Name;
Here is a detailed blog on this: https://www.c-sharpcorner.com/blogs/creating-arraylist-iterate-this-arraylist-in-jquery