You must Sign In to post a response.
  • Category: JQuery

    Create an array dynamically in jquery

    Hi,

    I want to create an array using jquery

    like

    var totals;

    totals = [0, 0, 0, 0, 0, 0];

    i want to create this dynamically

    for example

    var k=3

    then

    totals=[0,0,0] and so on.

    my requirement is like this

    var k=5

    for( k;k<5;k++)

    {

    }

    finally

    totals should be

    totals[0,0,0,0,0]

    how to do this in jquery

    Regards

    Baiju
  • #767702
    Hai Baiju,
    To create dynamic array in JQuery, you can use the push function which will push a new element to the existing array.
    existingArr.push(newVal);
    appendTo can also be use for the same.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767705
    In the JQuery you can keep on add the values by using the "push". That will handle your dynamic concept
    By Nathan
    Direction is important than speed

  • #767721
    Just define an single element array and use .push method to make it array with required elements, see below snippet

    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    fruits.push("Kiwi");

    The push() method adds new items to the end of an array, and returns the new length.
    Note: The new item(s) will be added at the end of the array.
    Note: This method changes the length of the array.
    Tip: To add items at the beginning of an array, use the unshift() method.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments