【问题标题】:EJS comparison between two same strings两个相同字符串之间的 EJS 比较
【发布时间】:2019-08-26 16:46:26
【问题描述】:

我尝试在 ejs 中比较两个在当前情况下相同的字符串,它运行 else 情况。代码如下:

<% var idf = notifications.data[i].from; %>
<% var ids = notifications.users[a].id; %>
<% if(idf == ids){ %>
    <% index = a; %>
    <script>alert('<%= index %>');</script>
    <% break; %>
<% }else{ %>
    <script>console.log('<%= idf %> - <%= ids %>');</script>
<% } %>

它记录 5d5ecfd1ad6d193de86c2264 - 5d5ecfd1ad6d193de86c2264

【问题讨论】:

    标签: javascript node.js ejs


    【解决方案1】:

    使用 === 而不是 == 来查看这是否可以解决问题,因为它会阻止内部类型强制。

    修剪字符串以查看是否添加了任何空白或填充可能也是值得的。

    【讨论】:

      【解决方案2】:

      您可以使用下面的代码来比较 ejs 中的两个字符串值

      <% if("some text" === "some text"){ %>
      <h1>Yes the above text are equal</h1>
      <% } %>
      

      【讨论】:

        【解决方案3】:

        如果你在比较 mongoDB objectId,那么使用 toString()。

        <% var idf = notifications.data[i].from; %>
        <% var ids = notifications.users[a].id; %>
        <% if(idf.toString() == ids.toString()){ %>
            <% index = a; %>
            <script>alert('<%= index %>');</script>
            <% break; %>
        <% }else{ %>
            <script>console.log('<%= idf %> - <%= ids %>');</script>
        <% } %>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多