MyTetra Share
Делитесь знаниями!
splice / вставка элементов / удаление элементов массива
Время создания: 27.09.2017 11:32
Текстовые метки: code
Раздел: javaScript - Объекты - Типы объектов - Массивы
Запись: xintrea/mytetra_db_mcold/master/base/1506501135g27fqgga5f/text.html на raw.githubusercontent.com

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>
Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования