// At position 2, remove 2 items:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,2);
NSB.Print "Removed:", returnVal
NSB.Print "New value", fruits
Splice: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
''array'': The name of an existing array. | ''array'': The name of an existing array. | ||
''index'': An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array. | ''index'': An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array. Starts from 0. | ||
''howmany'': The number of items to be removed. If set to 0, no items will be removed. | ''howmany'': The number of items to be removed. If set to 0, no items will be removed. | ||
Line 13: | Line 13: | ||
''item'': Optional. The new item(s) to be added to the array at index. | ''item'': Optional. The new item(s) to be added to the array at index. | ||
== Example | == Example == | ||
At position 2, remove 2 items: | <tabber> | ||
< | JavaScript= | ||
<syntaxhighlight lang="JavaScript"> | |||
// At position 2, remove 2 items: | |||
var fruits = ["Banana", "Orange", "Apple", "Mango"]; | |||
fruits.splice(2,2); | |||
NSB.Print "Removed:", returnVal | |||
NSB.Print "New value", fruits</syntaxhighlight> | |||
|-| | |||
BASIC= | |||
<syntaxhighlight lang="vb.net"> | |||
' At position 2, remove 2 items: | |||
fruits = ["Banana", "Orange", "Apple", "Mango"] | fruits = ["Banana", "Orange", "Apple", "Mango"] | ||
returnVal = Splice(fruits,2,2) | returnVal = Splice(fruits,2,2) | ||
Line 25: | Line 35: | ||
Splice(fruits,2,1,"Blueberry") | Splice(fruits,2,1,"Blueberry") | ||
Print fruits | Print fruits | ||
</ | </syntaxhighlight> | ||
</tabber> | |||
< | |||
== Output == | |||
Removed: Apple,Mango | Removed: Apple,Mango | ||
New value: Banana,Orange | New value: Banana,Orange | ||
== Related Items == | == Related Items == | ||
Line 51: | Line 49: | ||
[[Category:Variable Handling]] | [[Category:Variable Handling]] | ||
[[Category:BASIC Functions]] |
Latest revision as of 23:24, 24 July 2019
Splice(array, index, howmany[, item])
Description
The splice() method adds/removes items to/from an array, and returns the removed item(s). This function changes the original array.
array: The name of an existing array.
index: An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array. Starts from 0.
howmany: The number of items to be removed. If set to 0, no items will be removed.
item: Optional. The new item(s) to be added to the array at index.
Example
' At position 2, remove 2 items:
fruits = ["Banana", "Orange", "Apple", "Mango"]
returnVal = Splice(fruits,2,2)
Print "Removed:", returnVal
Print "New value", fruits
'Add a new item
Splice(fruits,2,1,"Blueberry")
Print fruits
Output
Removed: Apple,Mango New value: Banana,Orange