0

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}

enter image description here

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.

2 Answers 2

1

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

0
-1

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.

2
  • equals is in java and I'm asking in nodeJs. Apr 13, 2016 at 8:58
  • sorry read too fast, will update the answer (which is still similar) Apr 13, 2016 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.