微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在已定义的数组中查找值

如何解决如何在已定义的数组中查找值

关注此问题

trying to get the value of the struct inside an array

我有一个可接受的答案的地方,如果数组已定义,我试图获取该值...

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

因此,如果名称为“ form”,则获取值的详细信息

代码

    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 

解决方法

这看起来像是使用三元运算符。

<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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。