简体   繁体   English

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

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

I'm comparing two variables in Jade Template in my NodeJs (Express Framework) application. 我正在比较NodeJs(Express Framework)应用程序中Jade Template中的两个变量。 I can see that values are same but not sure why it's not working. 我可以看到值是相同的,但不确定为什么它不起作用。 Below is the piece of code. 下面是一段代码。

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}

在此处输入图片说明

You can see above, i showed both ids which is coming same for id=101 but still it is not adding selected attribute as per if block. 您可以在上面看到,我显示了两个ID,ID = 101相同,但仍未按if块添加所选属性。

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

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

String primitives and String objects also give different results when == A simple run like new String('a') == new String('a') would prove the point, use s_obj .valueOf() to convert into primitive and it would work 当==时,字符串基元和String对象也会产生不同的结果,例如new String('a')== new String('a')这样的简单运行将证明这一点,使用s_obj .valueOf()转换为基元,它将工作

Your are testing String equality with new String(...) == new String(...) which is not the way to go. 您正在使用new String(...) == new String(...)测试String相等性,这不是可行的方法。 You are actually comparing the String objects which are definitely not the same. 您实际上是在比较绝对不同的String 对象 In order to properly compare two strings you have to use new String(item.EmployerId).valueOf() === new String(ScheduleList.ProdEmployerId).valueOf() or String(item.EmployerId) === String(ScheduleList.ProdEmployerId) as String() converts an object to a String primitive. 为了正确比较两个字符串,您必须使用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