Interviews - C# Corner

Mahesh Chand
How to iterate through ArrayList in jQuery?
By Mahesh Chand in JQuery onAug 08 2019
  • Ankit Kanojia
    Aug, 2019 28

    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
  • Sagar Jaybhay
    Aug, 2019 20

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

    • 1
  • Ajinkyasingh Bais
    Sep, 2019 18

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

    • 0
  • Улисзс Рико
    Sep, 2019 11

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

    • 0
  • Ravi Patel
    Aug, 2019 22

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

    • 0
  • Binod Kumar
    Aug, 2019 21

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

    • 0
  • Bidyasagar Mishra
    Aug, 2019 11

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

    • 0
  • Arvind Singh Baghel
    Aug, 2019 9

    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

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS