Dizi Elemanını Silmek

İngilizce bir makaledir.

Dizi elemanını silmekDefinition and Usage

The splice() method is used to remove and add new elements to an array.

Syntax

arrayObject.splice(index,howmany,element1,.....,elementX)

 

Parameter Description
index Required. Specify where to add/remove elements. Must be a number
howmany Required Specify how many elements should be removed. Must be a number, but can be "0"
element1 Optional. Specify a new element to add to the array
elementX Optional. Several elements can be added

 


Example

In this example we will create an array and add an element to it:

<script type="text/javascript">
var arr = new Array(5);
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";
document.write(arr + "<br />");
arr.splice(2,0,"Lene");
document.write(arr + "<br />");
</script>

The output of the code above will be:

Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Lene,Stale,Kai Jim,Borge

 


Example

In this example we will remove the element at index 2 ("Stale"), and add a new element ("Tove") there instead:

<script type="text/javascript">
var arr = new Array(5);
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";
document.write(arr + "<br />");
arr.splice(2,1,"Tove");
document.write(arr);
</script>

The output of the code above will be:

Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Tove,Kai Jim,Borge

 


Example

In this example we will remove three elements starting at index 2 ("Stale"), and add a new element ("Tove") there instead:

<script type="text/javascript">
var arr = new Array(5);
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";
document.write(arr + "<br />");
arr.splice(2,3,"Tove");
document.write(arr);
</script>

The output of the code above will be:

Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Tove

The splice method of the JavaScript Array object can be used to add and/or remove elements from an array.

The splice method takes 1 to n parameters:

1: The array index at which changes will begin
2: How many array elements to remove
3-n: New array elements

var aryTest = [1,2,3,4];
document.write(aryTest.splice(2));

The preceding example uses splice to remove all elements after element 2 from the array. Note that the splice method returns the elements it has removed from the array, so in this case the return value is ‘3,4.’ The array itself, however, contains ‘1,2.’

A second parameter can be passed to splice to specify how many elements to remove from the array after the array index at which changes started.

var aryTest = [1,2,3,4];
document.write(aryTest.splice(2,1));

The preceding example uses splice to remove the third element from the array, leaving the array with ‘1,2,4.’

After the second parameter, additional parameters can be provided to add items to the array in the place were elements were removed.

var aryTest = [1,2,3,4];
document.write(aryTest.splice(2,1,7));

The preceding example will remove 3 from the array and replace it will 7.

The splice method is used to add and/or remove elements of an array returning an array of the elements removed. You first need to specify the index at which you wish to start removing elements, and the number to remove. (if 'howMany' is 0, you should specify at least 1 element to add). The following code creates an array 'cars' and then displays two elements starting with 'cars[1]':
 
Code:
cars = ["Mercedes", "Ford", "Chrysler", "Honda", "Volvo"]
document.write(cars.splice(1,2))

 
Output:
Ford,Chrysler
 
You can also include optional new elements to replace the ones removed. Expanding on the previous example, the following code would create an array consisting of the two extracted elements "Ford" and "Chrysler", but replace them with "Citreon" in the original array:
 
Code:
cars = ["Mercedes", "Ford", "Chrysler", "Honda", "Volvo"]
removed_cars = cars.splice(1, 2, "Citreon")
document.write(removed_cars + "<BR>")
document.write(cars)

 
Output:
Ford,Chrysler
Mercedes,Citreon,Honda,Volvo

Kaynak blog.techsaints.com/2007/05/04/javascript-splice-method-array-add-and-remove/ , www.devguru.com/Technologies/ecmascript/QuickRef/splice.html , www.w3schools.com/jsref/jsref_splice.asp


Yorumunuzu Ekleyin
Dizi Elemanını Silmek Yorumları +1 Yorum
  • Saffet
    1
    Saffet
    İngilizce bilsek Türkçe kaynak aramaz insanlar biraz akıl.
    15 Ocak 2020 19:24:24, Çarşamba

Javascript Ve Ses
Fonksiyonlar
Yükleniyor...