繁体   English   中英

NodeJs Express框架应用程序中Jade Template中的字符串比较问题

[英]String comparison issue in Jade Template in NodeJs Express framework application

我正在比较NodeJs(Express Framework)应用程序中Jade Template中的两个变量。 我可以看到值是相同的,但不确定为什么它不起作用。 下面是一段代码。

select#corpid.form-control.grid-select(name='corpid', style='display: none', value='#{ScheduleList.CorpId}', onchange="setEmployer(this)")
    each item in Employers
        if(new String(item.EmployerId) == new String(ScheduleList.ProdEmployerId))
            option(value = '#{item.EmployerId}', selected='selected') #{item.AccountName}
        else
            option(value = '#{item.EmployerId}', eid="#{new String(item.EmployerId)}", pid="#{new String(ScheduleList.ProdEmployerId)}") #{item.AccountName}

在此处输入图片说明

您可以在上面看到,我显示了两个ID,ID = 101相同,但仍未按if块添加所选属性。

var s_prim ='foo'; var s_obj = new String(s_prim);

console.log(typeof s_prim); //记录“字符串” console.log(typeof s_obj); //记录“对象”

当==时,字符串基元和String对象也会产生不同的结果,例如new String('a')== new String('a')这样的简单运行将证明这一点,使用s_obj .valueOf()转换为基元,它将工作

您正在使用new String(...) == new String(...)测试String相等性,这不是可行的方法。 您实际上是在比较绝对不同的String 对象 为了正确比较两个字符串,您必须使用new String(item.EmployerId).valueOf() === new String(ScheduleList.ProdEmployerId).valueOf()String(item.EmployerId) === String(ScheduleList.ProdEmployerId)作为String()将对象转换为String原语。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM