On Oct 17, 2019, at 5:06 PM, Chip Scheide via 4D_Tech <[email protected]> 
wrote:
> 
> Given that I have an array of objects - Array Object(object_array;5)
> 
> (hand waving) I populate the array with objects ...
> 
> how do I find which object in the array has an attribute/property with 
> a specific value?
> ex: property "Object_Name" : "Fred"
> 
> How do I find the array element (object) in which the property 
> 'Object_Name = "Fred”?

Option 1: update to v17 so you can use a collection, because functionality 
supporting object arrays will probably never be added. Collections have a 
.query() method you can use to search property values.

Option 2: you write your own method to find in an object array, where you have 
to take the search value as a pointer unless you want to write a method for 
each value type you intend to search for.

Since I did option 2 a couple years before option 1, here’s some code. You’ll 
need Cannon Smith’s OBJ methods as well. 

  // ----------------------------------------------------
  // User name (OS): James Crate
  // Date and time: 07/19/16, 16:37:33
  // ----------------------------------------------------
  // Method: OBJA_Find_in_Array
  // 
  // finds in object array where value for key matches specified value
  //
  // ----------------------------------------------------
C_LONGINT($0;$foundIdx)
$foundIdx:=-1
C_POINTER($1;$objArrPtr)
$objArrPtr:=$1
C_TEXT($2;$key)  //Can use dot notation
$key:=$2
C_POINTER($3;$searchValuePtr)
$searchValuePtr:=$3
C_LONGINT($4;$startIdx)
If (Count parameters>=4)
        $startIdx:=$4
Else 
        $startIdx:=1
End if 
  // ----------------------------------------------------
C_LONGINT($longintVal;$searchType;$i)
C_REAL($realVal)
C_DATE($dateVal)
C_TIME($timeVal)
C_TEXT($textVal)
C_POINTER($valPtr)
  // ----------------------------------------------------

$searchType:=Type($searchValuePtr->)
For ($i;$startIdx;Size of array($objArrPtr->);1)
        $obj:=$objArrPtr->{$i}
        
        Case of 
                : (($searchType=Is text) | ($searchType=Is alpha field))
                        $textVal:=OBJ_Get_Text ($obj;$key)
                        $valPtr:=->$textVal
                        
                : (($searchType=Is longint) | ($searchType=Is integer) | 
($searchType=Is integer 64 bits))
                        $longintVal:=OBJ_Get_Long ($obj;$key)
                        $valPtr:=->$longintVal
                        
                : (($searchType=Is real) | ($searchType=Is float))
                        $realVal:=OBJ_Get_Real ($obj;$key)
                        $valPtr:=->$realVal
                        
                : ($searchType=Is date)
                        $dateVal:=OBJ_Get_Date ($obj;$key)
                        $valPtr:=->$dateVal
                        
                : ($searchType=Is time)
                        $timeVal:=OBJ_Get_Time ($obj;$key)
                        $valPtr:=->$timeVal
                Else 
                        ASSERT(False;"This type cannot be compared.")
        End case 
        
        If ($valPtr->=$searchValuePtr->)
                $foundIdx:=$i
                $i:=Size of array($objArrPtr->)
        End if 
End for 

$0:=$foundIdx
  // ----------------------------------------------------

Jim Crate

**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[email protected]
**********************************************************************

Reply via email to