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

Home

 
4D v19.8
collection.findIndex( )

collection.findIndex( ) 


 

collection.findIndex ( {startFrom ;} methodName {; param {; param2 ; ... ; paramN}} ) -> Function result 
Parameter Type   Description
startFrom  Longint in Index to start the search at
methodName  Text in Name of the function to call for the find
param  Expression in Parameter(s) to pass to methodName
Function result  Longint in Index of first value found, or -1 if not found

The collection.findIndex( ) method returns the index, in the collection, of the first value for which methodName, applied on each element, returns true. The method returns -1 if no element of the collection was evaluated to true.

Note: This method does not modify the original collection.

By default, collection.findIndex( ) searches in the whole collection. Optionally, you can pass in startFrom the index of element from which to start the search.

  • If startFrom >= the collection's length, -1 is returned, which means the collection is not searched.
  • If startFrom < 0, it is considered as the offset from the end of the collection (startFrom:=startFrom+length).
    Note: Even if startFrom is negative, the collection is still searched from left to right.
  • If startFrom = 0, the whole collection is searched (default).

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, using or not the parameter(s). This method receives an Object parameter in $1 and must set $1.result to true for the first element fulfilling the condition.

methodName receives the following parameters:

  • in $1.value: element value to be evaluated
  • in $2: param
  • in $N...: param2...paramN

methodName sets the following parameter(s):

  • $1.result (boolean): true if the element value matches the search condition.
  • $1.stop (boolean, optional): true to stop the method callback. The returned value is the last calculated.

Example  

You want to find the position of the first city name within a collection:

 C_COLLECTION($c)
 C_LONGINT($val2;$val3)
 $c:=New collection
 $c.push(New object("name";"Cleveland";"zc";35049))
 $c.push(New object("name";"Blountsville";"zc";35031))
 $c.push(New object("name";"Adger";"zc";35006))
 $c.push(New object("name";"Clanton";"zc";35046))
 $c.push(New object("name";"Clanton";"zc";35045))
 $val2:=$c.findIndex("FindCity";"Clanton") // $val2=3
 $val3:=$c.findIndex($val2+1;"FindCity";"Clanton") //$val3=4

The code for the FindCity method is:

 C_OBJECT($1)
 C_TEXT($2)
 $1.result:=$1.value.name=$2



See also 

collection.find( )

 
PROPERTIES 

Product: 4D
Theme: Collections
Number: 805256

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)