Search code examples
arrayscoldfusionlucee

How to find a Value in the defined Array


Following Up this Question

trying to get the value of the struct inside an array

Where i had an accepted answer, I am trying to get the value if the array is defined...

https://cffiddle.org/app/file?filepath=a5a33cf1-1304-4423-ad18-f77a7ea14c98/06ec1e7f-3de5-41bc-b155-3eff2afc8a0f/a420c091-6f53-4ef3-882d-50506284349e.cfm

so if the name is "form", get the details of the value

The Code

    but my structure is like this 
    
    <cfscript>
        x = [];
        x[1] = {};
        x[1]["name"] = "form";
        x[1]["value"] = "100";
writedump(x);           
    </cfscript>
    
    <cfset formIndex = ArrayFind(x, function(st){ 
        return st.name == "form"; 
    })>
    
    <cfif formIndex eq 1>
        <cfset value = x.filter((item) => ( item.value) )>
    </cfif>
    <cfdump value="#value#">
    <cfdump var="#formindex#">
    
    as i have to check form the name = "form" and then only i need to get the value of value field, else it should return me 0 

Solution

  • This looks like a use of the ternary operator.

    <cfscript>
        x = [];
        x[1] = {};
        x[1]["name"] = "form";
        x[1]["value"] = "100";
         
    
            formIndex = x.find((st) => (st.name == "form"));
            
            result = formIndex ? x[formIndex].value : 0
    </cfscript>