Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptAlternative of Array splice() method in JavaScript

Alternative of Array splice() method in JavaScript

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;">
        neveropen
    </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;">
        neveropen
    </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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments