Question
· Apr 30, 2021

cache/iris class array approach analogue to javascript Associative array

objects in javascript :

var oM = new Object;

var mMode=["down","up","click"] ;
var key,val ;
    for (key in mMode)
    {

       val = mMode[key] ;
        oM[val]={} ;
        oM[val].id   ="" ;
        oM[val].type ="" ;

       ...

 }

so I can access the .type directly as :

oM.down.type

or:

oM["down"].type

what is the syntax in CACHE or IRIS, to achieve such approach via class/key/property...?

acctually: class/object/key/property anlaogue to ^GLOBAL(key,"property")=val

Discussion (9)1
Log in or sign up to continue

I think you're looking for the %ArrayOfObjects class for this one. You'd create your objects with all of their value, ID, and type properties, then you'd create the array:

set array = ##class(%ArrayOfObjects).%New()

Then you set values of the array using the SetAt method:

do array.SetAt(downobject,"down")

Then to access a particular value, you use the GetAt method, then dot syntax to access the object's properties:

set myid = array.GetAt("down").id

Here's the %ArrayOfObjects class documentation.

ClassMethod TestObj() As %DynamicObject

{
        Set oM = {}
        Set mMode = ["down","up","click"]
        Set iter = mMode.%GetIterator()
        While iter.%GetNext(,.val)
        {
            Do oM.%Set(val,{"id":"","type":""})
        }
        Quit oM
}

USER> set oM = ##class(User.DynObj).TestObj()
USER> write oM.%ToJSON()
{"down":{"id":"","type":""},"up":{"id":"","type":""},"click":{"id":"","type":""}}
USER> zwrite oM.down.id
""
USER> zwrite oM.up.type
""
USER> set oM.click.type = "double"
USER> write oM.click.type
double
USER> write oM.%ToJSON()
{"down":{"id":"","type":""},"up":{"id":"","type":""},"click":{"id":"","type":"double"}}