Forum Discussion

rome_castillo's avatar
rome_castillo
Occasional Contributor
10 years ago

Can I pass the object Property Name as a parameter

 I have used FindAllChildren to get all the children of a window object and put it in an array (objArray).  I am trying to pass the Property Name (myName) and Property Value (myValue) to find the index in the array where a match is found.

 

Here's part of the JScript code:

 

function getIndex(objArray, myName, myValue)
{
    var i;

    for(i=0; i<=objArray.length; i++)
    {
        if(objArray[i].myName == myValue)

        ....
    {

    ....

}

 

This function is failing since it is looking for "myName" Property Name instead of the string that was passed (ie I was trying to pass "ClrClassName" to myName).

 

Is there a way to use pass the Property Name as a parameter for the above use?

 

Thanks!

  • function getIndex(objArray, myName, myValue)
    {
        for(var i = 0; i < objArray.length; i++){
            if(objArray[i][myName] == myValue){
            //do stuff
    } { }

     

  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    function getIndex(objArray, myName, myValue)
    {
        for(var i = 0; i < objArray.length; i++){
            if(objArray[i][myName] == myValue){
            //do stuff
    } { }

     

    • rome_castillo's avatar
      rome_castillo
      Occasional Contributor

      Thanks Ryan!  That was so simple I can't believe I wasn't able to figure that out.  Really appreciate the help!

      • Ryan_Moran's avatar
        Ryan_Moran
        Valued Contributor

        No prob :).

        You may also run into a scenario in which some objects returned by the .Find / .FindAll methods do not have the property you want to check. In this case you can verify that the object has the specified property with aqObject.IsSupported(myName, objArray[i]).