Splice: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
(Created page with "Splice(''array'', ''index'', ''how many''[, ''item'']) == Description == The splice() method adds/removes items to/from an array, and returns the removed item(s). Note: Thi...")
 
No edit summary
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
Splice(''array'', ''index'', ''how many''[, ''item''])
Splice(''array'', ''index'', ''howmany''[, ''item''])


== Description ==
== Description ==


The splice() method adds/removes items to/from an array, and returns the removed item(s).
The splice() method adds/removes items to/from an array, and returns the removed item(s). This function changes the original array.


Note: This method changes the original array.
''array'': The name of an existing array.


''index'': Required. 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.


''how many'' Required. 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.


''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 (Basic) ==
== Example ==


<pre>
<tabber>
Rem UBound Example
JavaScript=
'UBound returns upper bound of array dimension
<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"]
returnVal = Splice(fruits,2,2)
Print "Removed:", returnVal
Print "New value", fruits


Dim Other, Children(3), Parents(3, 1)
'Add a new item
Other = Array("Damien", "Pip", "Wendy")
Splice(fruits,2,1,"Blueberry")
Print "'Other' Upper Bound:", Ubound(Other)
Print fruits
Print "'Children' Upper Bound:", _
</syntaxhighlight>
      UBound(Children, 1)
</tabber>
Print "'Parents' Upper Bounds:", _
      UBound(Parents), UBound(Parents, 2)
</pre>
 
== Example (JavaScript) ==
<pre>
// UBound Example
/* Length returns upper bound of array dimension + 1 */
 
var Other, Children=new Array(3), Parents=new Array(3,1);
Other= new Array("Damien" , "Pip" , "Wendy");
Parents[3]=''; //need to assign variable to use length on multidim array
NSB.Print("'Other' Upper Bounds: " + ((Other.length)-1).toString());
NSB.Print("'Children' Upper Bounds: " + ((Children.length)-1).toString());
NSB.Print("'Parents' Upper Bounds: " + ((Parents.length)-1).toString());
</pre>


== Output ==
== Output ==
 
Removed: Apple,Mango
<pre>
New value: Banana,Orange
'Other' Upper Bound:2
'Children' Upper Bound:   3
'Parents' Upper Bounds:    3      1
</pre>


== Related Items ==
== Related Items ==


[[array|Array]], [[dim|Dim]], [[lbound|LBound]], [[redim|ReDim]]
[[array|Array]], [[dim|Dim]], [[lbound|LBound]], [[push|Push]], [[redim|ReDim]]


[[Category:Language Reference]]
[[Category:Language Reference]]


[[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:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,2);
NSB.Print "Removed:", returnVal
NSB.Print "New value", fruits

' 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

Related Items

Array, Dim, LBound, Push, ReDim