This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v19.8
collection.slice( )

collection.slice( ) 


 

collection.slice ( startFrom {; end} ) -> Function result 
Parameter Type   Description
startFrom  Longint in Index to start the search at (included)
end  Longint in End index (not included)
Function result  Collection in New collection containing sliced elements (shallow copy)

The collection.slice( ) method returns a portion of a collection into a new collection, selected from startFrom index to end index (end not included). This method returns a shallow copy of the collection. If the original collection is a shared collection, the returned collection is also a shared collection.

Note: This method does not modify the original collection.

The returned collection contains the element specified by startFrom and all subsequent elements up to, but not including, the element specified by end. If only the startFrom parameter is specified, the returned collection contains all elements from startFrom to the last element of the original collection.

  • If startFrom < 0, it is recalculated as startFrom:=startFrom+length (it is considered as the offset from the end of the collection).
  • If the calculated value < 0, startFrom is set to 0.
  • If end < 0 , it is recalculated as end:=end+length.
  • If end < startFrom (passed or calculated values), the method does nothing. 

Example  

 C_COLLECTION($c;$nc)
 $c:=New collection(1;2;3;4;5)
 $nc:=$c.slice(0;3) //$nc=[1,2,3]
 $nc:=$c.slice(3) //$nc=[4,5]
 $nc:=$c.slice(1;-1) //$nc=[2,3,4]
 $nc:=$c.slice(-3;-2) //$nc=[3]



See also 

collection.extract( )

 
PROPERTIES 

Product: 4D
Theme: Collections
Number: 805250

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v16 R6

 
ARTICLE USAGE

4D Language Reference ( 4D v19)
4D Language Reference ( 4D v19.1)
4D Language Reference ( 4D v19.4)
4D Language Reference ( 4D v19.5)
4D Language Reference ( 4D v19.6)
4D Language Reference ( 4D v19.7)
4D Language Reference ( 4D v19.8)