This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v19.8
collection.filter( )
|
collection.filter ( methodName {; param}{; param2 ; ... ; paramN} ) -> Function result | ||||||||
Parameter | Type | Description | ||||||
methodName | Text |
![]() |
Name of the function to call to filter the collection | |||||
param | Expression |
![]() |
Parameter(s) to pass to methodName | |||||
Function result | Collection |
![]() |
New collection containing filtered elements (shallow copy) | |||||
The collection.filter( ) method returns a new collection containing all elements of the original collection for which methodName method result is true. This method returns a shallow copy, which means that objects or collections in both collections share the same reference. 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.
In methodName, pass the name of the method to use to evaluate collection elements, along with its parameter(s) in param (optional). methodName can perform any test, with or without the parameter(s), and must return true in $1.result for each element fulfilling the condition and thus, to push to the new collection.
methodName receives the following parameters:
methodName sets the following parameter(s):
You want to get the collection of text elements whose length is smaller than 6:
C_COLLECTION($col)
C_COLLECTION($colNew)
$col:=New collection("hello";"world";"red horse";66;"tim";"san jose";"miami")
$colNew:=$col.filter("LengthLessThan";6)
//$colNew=["hello","world","tim","miami"]
The code for LengthLessThan method is:
C_OBJECT($1)
C_LONGINT($2)
If(Value type($1.value)=Is text)
$1.result:=(Length($1.value))<$2
End if
You want to filter elements according to their value type:
C_COLLECTION($c)
$c:=New collection(5;3;1;4;6;2)
$c.push(New object("name";"Cleveland";"zc";35049))
$c.push(New object("name";"Blountsville";"zc";35031))
$c2:=$c.filter("TypeLookUp";Is real) // $c2=[5,3,1,4,6,2]
$c3:=$c.filter("TypeLookUp";Is object)
// $c3=[{name:Cleveland,zc:35049},{name:Blountsville,zc:35031}]
The code for TypeLookUp is:
C_OBJECT($1)
C_LONGINT($2)
If(OB Get type($1;"value")=$2)
$1.result:=True
End if
Product: 4D
Theme: Collections
Number:
705249
Created: 4D v16 R6
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)