简体   繁体   中英

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

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

console.log(typeof s_prim); // Logs "string" 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

Your are testing String equality with new String(...) == new String(...) which is not the way to go. You are actually comparing the String objects which are definitely not the same. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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