Alternative of Array splice() method in JavaScript - GeeksforGeeks

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.
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:
sooda367
JavaScript-Methods
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to filter object array based on attributes?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript