Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • javascript:How to insert values in an array

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 320
    Comment on it

    hii,

    We can insert mutliple value in  an array using different method as per requirements.

    Here is the list of method we can use to insert values in an array.

    • Array fill() Method :fill() method is used when we want to fills either all the elements in an array with a static value replacing the pre-existing values or  values will be filled from the defined index  using starting fill and ending fill.

          Example:

    1.  In this example all the values in an array will be filled with a static value.
    <button onclick="fillAll()">Try it</button>
    
    <p id="fillArray"></p>
    
    <script>
    var languages = ["Hindi", "Chinese", "French"];
    document.getElementById("fillArray").innerHTML = languages;
    
    function fillAll() {  
        document.getElementById("fillArray").innerHTML = languages.fill("English");
    }
    </script>
    
    

       

    2. In this example  values will be filled from the defined index  using starting fill and ending fill with a static value,where starting fill will define the index to start filling the array and ending fill will define the index to stop filling the array.

        Syntax:array.fill(value,start,end)

    
    
    <button onclick="fillValue()">Try it</button>
    
    <p id="fill"></p>
    
    <script>
    var languages = ["Hindi", "Chinese", "French"];
    document.getElementById("fill").innerHTML = languages;
    
    function fillValue() {  
        document.getElementById("fill").innerHTML = languages.fill("English,0,2");
    }
    </script>
    
    
    • Array push() Method : push() method is used when we want to add new items to the end of an array.While adding items in an array length of array increases.

             Using push() method we can add one or more than one items.

             Example: In this example  new elements will be added to the an array using push method.

    <button onclick="pushData()">Try it</button>
    
    <p id="push"></p>
    
    <script>
    var languages = ["Hindi", "Chinese", "French"];
    document.getElementById("push").innerHTML = languages;
    
    function pushData() {
         languages.push("English", "Arabic", "Spanish");
         document.getElementById("push").innerHTML = languages;
    
    }
    </script>

     

    • Array unshift() Method : unshift() method is used when we want to add new items to the beginning of an array.While adding items in an array length of array increases.

             Using unshift() method we can add one or more than one items.

           Example: In this example  new elements will be added to the beginning of an array using                       unshift() method.

    
    
    <button onclick="fillBeginning()">Try it</button>
    
    <p id="fillArrayBeginning"></p>
    
    <script>
    var languages = ["Hindi", "Chinese", "French"];
    document.getElementById("fillArrayBeginning").innerHTML = languages;
    
    function fillBeginning() {  
        languages.unshift("English", "Spanish");
        document.getElementById("fillArrayBeginning").innerHTML = languages;
    }
    </script>
    
    [Click and drag to move]
    

     

    • Array splice() Method : splice() method is used when we want to either add  items to an array  or remove items from an array. splice() method returns the removed item(s) and changes the original array if any item is removed otherwise array remains the same.

           Syntax: array.splice(index,no.of items,item1,.....,itemX)

          ----->  In splice() method three parameter values is required.

          ----->  Index value is required to identify position of items to be added or removed.

         ------>  No. of items to be removed need to be specified. If no items will be removed  set  it to 0.

          ----->  itemX specifies new item(s) that will be added to the array.   

        

    Example:

                        In this example items will be either removed or added to an array.

    <button onclick="splice()">Try it</button>
    
    <p id="spliceArray"></p>
    
    <script>
    var languages = ["Hindi", "Chinese", "French"];
    document.getElementById("spliceArray").innerHTML = languages;
    
    function splice() {  
        languages.splice(2, 0, "English", "Spanish");
        document.getElementById("spliceArray").innerHTML = languages;
    }
    </script>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: