8
Reply

How to iterate through ArrayList in jQuery?

Mahesh Chand

Mahesh Chand

5y
13.3k
13
Reply

    You can have multiple option to do loop, see below examples

    1. var arrayCollection = ["ABC", "DEF", "HIJ"];
    2. $.each(arrayCollection, function(index, value){
    3. console.log("index : ",index, " value :", value);
    4. });
    5. $(arrayCollection).each(function(index, value){
    6. console.log("index : ",index, " value :", value);
    7. });
    8. for(var i = 0; i <; arrayCollection.length; i++) {
    9. console.log("index : ", i , " value :", arrayCollection[i]);
    10. }
    11. $.map(arrayCollection, function( val, i ) {
    12. console.log("index : ", i , " value :", val);
    13. });
    14. arrayCollection.map((val, i ) =>; console.log("index : ", i , " value :", val));

    1. var Array = ["A", "B", "C", "D", "E"];
    2. $.each(Array, function(index, value){
    3. $("#result").append(index + ": " + value + '<;br>;');
    4. });`

    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 in
    eg. var numbers = [1, 2, 3, 4];
    for(var i in numbers)
    {
    console.log(“loop”, numbers[i]);
    }

    4.for of
    eg. var numbers = [1, 2, 3, 4];
    for(var i of numbers)
    {
    console.log(“loop”, numbers[i]);
    }

    5.forEach
    eg. 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 index
    for(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);
    });

    1. $.each([ 52, 97 ], function( index, value ) {
    2. alert( index + ": " + value );
    3. });

    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.

    1. var Ids=1;
    2. var naming="ABC";
    3. var DataList=[{ "Id":1, "Name":"ABC" }, {"Id":2, "Name":"XYZ"}];
    4. $.each( DataList, function( key, value ) {
    5. Ids =value.Id;
    6. naming =value.Name;
    7. });

    Here is a detailed blog on this: https://www.c-sharpcorner.com/blogs/creating-arraylist-iterate-this-arraylist-in-jquery