Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Alternative of Array splice() method in JavaScript

  • Last Updated : 09 Aug, 2021

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Example:

javascript




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

 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:

javascript




<!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, 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>
</html>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!