Open In App

Alternative of Array splice() method in JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

The array splice() method is a method of JavaScript and In this article, we are discussing what are alternatives to 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 into a new array. Return this new array after the loop ends.

Example: This example shows the above-explained approach.

HTML




<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>


 Output:

Alternative of Array splice() method

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

Example: This example shows the above-explained approach.

HTML




<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, insert ) {
          
            var rem = this.slice( s, s + tRemv );
            var temp = 
         this.slice(0, s).concat( insert, 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>


Output:

Alternative of Array splice() method



Last Updated : 09 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads