Skip to content
Related Articles

Related Articles

Alternative of Array splice() method in JavaScript
  • Last Updated : 31 Jul, 2020

Array splice() method is a method of JavaScript and In this articles we are discussing what are alternatives of this method. Here are 2 examples discussed below.

Approach 1: In this approach, the startIndex(From where to start removing the elements) and count(Number of elements to remove) are the variables. If the count is not passed then treat it as 1. Run a while loop till the count is greater than 0 and start removing the desired elements and push it in a new array. Return this new array after the loop ends.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html> 
<head> 
    <title> 
        Alternative of Array splice() method in JavaScript
    </title> 
</head> 
<body style="text-align:center;"
    <h1 style="color:green;">  
            GeeksforGeeks  
    </h1> 
    <p id="GFG_UP"
    </p> 
    <button onclick="myGFG()"
        Click Here 
    </button> 
    <p id="GFG_DOWN"
    </p> 
    <script> 
        var arr = [0, 3, 1, 5, 2, 7, 4, 9, 10];
        var up = document.getElementById("GFG_UP"); 
        up.innerHTML = 
"Alternative of Array splice() method in JavaScript."+
             " <br>Array = [" + arr + "]";
        var down = document.getElementById("GFG_DOWN");
        function mySplice(arr, ind, ct) {
          // if ct(count) not passed in function call.
          if (typeof ct == 'undefined') {
          ct = 1;
          }
          var rem = [];
          while (ct--) {
            var indRem = ind + CT;
          //pushing the elements rem array
            rem.push(arr[indRem]);
         // removing the element from original array.
            arr[indRem] = arr.pop(); 
          }
         // returning the removed elements
          return rem; 
        }
        function myGFG() {
        down.innerHTML = 
          "Removed Elements - " + mySplice(arr, 4, 3);
        
    </script> 
</body> 
</html>

chevron_right


 Output:



Approach 2: In this approach, slice() method is used to get the removed elements and this method is also used to get the output array. This approach also takes argument to insert the new elements to the array.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html> 
<head> 
    <title> 
        Alternative of Array splice()
        method in JavaScript
    </title> 
</head> 
<body style="text-align:center;"
    <h1 style="color:green;">  
            GeeksforGeeks  
    </h1> 
    <p id="GFG_UP"
    </p> 
    <button onclick="myGFG()"
        Click Here 
    </button> 
    <p id="GFG_DOWN"
    </p> 
    <script> 
        var arr = [0, 3, 1, 5, 2, 7, 4, 9, 10];
        var up = document.getElementById("GFG_UP"); 
        up.innerHTML = "Alternative of Array splice() "+
        "method in JavaScript.<br>Array = [" + arr + "]";
        var down = document.getElementById("GFG_DOWN");
        Array.prototype.altSplice = function(s, tRemv, insrt ) {
  
            var rem = this.slice( s, s + tRemv );
            var temp = 
         this.slice(0, s).concat( insrt, this.slice( s + tRemv ) );
            this.length = 0;
            this.push.apply(this, temp );
            return rem;
        };
        function myGFG() {
            down.innerHTML = 
            "Splice result - "
             arr.altSplice(3, 2, 6) +
              "<br>Original array - " + arr;
        
    </script> 
</body> 
</html>

chevron_right


Output:

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :