Apa itu splice di javascript?

Below is the example of Array splice() method. 

Example: 

Javascript

<script>

    var webDvlop = ["HTML", "CSS", "JS", "Bootstrap"];

    document.write(webDvlop + "<br>");

    var removed = webDvlop.splice(2, 1, 'PHP', 'React_Native')

    document.write(webDvlop + "<br>");

    document.write(removed + "<br>");

    webDvlop.splice(-2, 0, 'React')

    document.write(webDvlop)

</script>               

Output: 

HTML,CSS,JS,Bootstrap
HTML,CSS,PHP,React_Native,Bootstrap
JS
HTML,CSS,PHP,React,React_Native,Bootstrap

The arr.splice() method is an inbuilt method in JavaScript which is used to modify the contents of an array by removing the existing elements and/or by adding new elements.

Syntax:  

Array.splice( index, remove_count, item_list )

Parameter: This method accepts many parameters some of them are described below: 

  • index: It is a required parameter. This parameter is the index which start modifying the array (with origin at 0). This can be negative also, which begins after that many elements counting from the end.
  • remove_count: The number of elements to be removed from the starting index.
  • items_list: The list of new items separated by comma operator that is to be inserted from the starting index.

Return Value: While it mutates the original array in-place, still it returns the list of removed items. In case there is no removed array it returns an empty array.

Below example illustrates the Array.splice() method in JavaScript:

Example:  

Javascript

<script>

var languages = ['C++', 'Java', 'Html', 'Python', 'C'];

document.write(languages + "<br>");

var removed = languages.splice(2, 1, 'Julia', 'Php')

document.write(languages + "<br>");

document.write(removed + "<br>");

languages.splice(-2, 0, 'Pascal')

document.write(languages)

</script>                   

Output: 

C++,Java,Html,Python,C
C++,Java,Julia,Php,Python,C
Html
C++,Java,Julia,Php,Pascal,Python,C 

 Supported Browser:

  • Google Chrome 1 and above
  • Internet Explorer 5.5 and above
  • Firefox 1 and above
  • Opera 4 and above
  • Safari 1 and above



Description

Javascript array splice() method changes the content of an array, adding new elements while removing old elements.

Syntax

Its syntax is as follows −

array.splice(index, howMany, [element1][, ..., elementN]);

Parameter Details

  • index − Index at which to start changing the array.

  • howMany − An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.

  • element1, ..., elementN − The elements to add to the array. If you don't specify any elements, splice simply removes the elements from the array.

Return Value

Returns the extracted array based on the passed parameters.

Example

Try the following example.

<html>
   <head>
      <title>JavaScript Array splice Method</title>
   </head>
   
   <body>   
      <script type = "text/javascript">
         var arr = ["orange", "mango", "banana", "sugar", "tea"];         
         var removed = arr.splice(2, 0, "water");
         document.write("After adding 1: " + arr );
         document.write("<br />removed is: " + removed);
         
         removed = arr.splice(3, 1);
         document.write("<br />After adding 1: " + arr );
         document.write("<br />removed is: " + removed);
      </script>      
   </body>
</html>

Output

After adding 1: orange,mango,water,banana,sugar,tea
removed is: 
After adding 1: orange,mango,water,sugar,tea
removed is: banana 

javascript_arrays_object.htm

Apa itu splice pada JavaScript?

splice() merupakan method array di JavaScript yang berfungsi untuk mengubah elemen array termasuk menghapus, mengganti, atau menambah elemen baru. Method ini mengubah array asli dan mengembalikan array berisi elemen yang dihapus.

Apa itu array splice?

Method Objek Array: Array.splice() Method splice() adalah method 'serba-bisa' yang bisa digunakan untuk memotong array, menambahkan elemen array, bahkan melakukan keduanya sekaligus. Tidak seperti method slice() dan concat(), pemanggilan method ini akan mengubah array asal.