检查数组是否包含对象nodejs

时间:2020-04-25 20:59:37

标签: arrays node.js mongodb object mongoose

我试图内联查看mongodb数据库中的数组是否包含特定对象,但是我无法使其正常工作。

我一直在寻找不同的解决方案,并尝试了所有解决方案; Array.some,Array.includes,Array.indexOf等。

我对此很陌生,所以我可能会误会了,有人知道我在做什么错吗?该应用程序正常启动,没有错误消息,但它表示每个人都是“领导者”,而并非如此。

<div class="ui three buttons">
        <% if(task.leaders.includes(currentUser)){ %>
          <button form="deleteTaskForm" class="ui basic negative button"><i class="ui icon trash alternate"></i></button> 
          <a class="ui basic orange button" href="/task/<%= task._id %>/edit"><i class="ui icon edit"></i></a> <% }
          else{ %>
          <div style="opacity: 40%;" class="ui basic negative button popupButton" data-content="Only the leader of the task can remove this task."><i class="ui icon trash alternate"></i></div>
          <div style="opacity: 40%;" class="ui basic orange button popupButton" data-content="Only the leader of the task can edit this task."><i class="ui icon edit"></i></div>
          <% } %>
        <button form="doneTaskForm" class="ui basic green button"><i class="ui icon check square"></i></button>
      </div>

Array.prototype.some()

<div class="ui three buttons">
        <% if(task.leaders.some(leader => leader._id == currentUser._id)){ %>
          <button form="deleteTaskForm" class="ui basic negative button"><i class="ui icon trash alternate"></i></button> 
          <a class="ui basic orange button" href="/task/<%= task._id %>/edit"><i class="ui icon edit"></i></a> <% }
          else{ %>
          <div style="opacity: 40%;" class="ui basic negative button popupButton" data-content="Only the leader of the task can remove this task."><i class="ui icon trash alternate"></i></div>
          <div style="opacity: 40%;" class="ui basic orange button popupButton" data-content="Only the leader of the task can edit this task."><i class="ui icon edit"></i></div>
          <% } %>
        <button form="doneTaskForm" class="ui basic green button"><i class="ui icon check square"></i></button>
      </div>

编辑:已解决。这是使它对我有用的解决方案。

<div class="ui three buttons">
        <% if(task.leaders.some(leader => leader._id.equals(currentUser._id))){ %>
          <button form="deleteTaskForm" class="ui basic negative button"><i class="ui icon trash alternate"></i></button> 
          <a class="ui basic orange button" href="/task/<%= task._id %>/edit"><i class="ui icon edit"></i></a> <% }
          else{ %>
          <div style="opacity: 40%;" class="ui basic negative button popupButton" data-content="Only the leader of the task can remove this task."><i class="ui icon trash alternate"></i></div>
          <div style="opacity: 40%;" class="ui basic orange button popupButton" data-content="Only the leader of the task can edit this task."><i class="ui icon edit"></i></div>
          <% } %>
        <button form="doneTaskForm" class="ui basic green button"><i class="ui icon check square"></i></button>
      </div>

1 个答案:

答案 0 :(得分:0)

比较对象是JavaScript上的繁重任务,您需要比较唯一对象属性的解决方案

if (task.leaders.some(lead => lead.user.id == currentUser.id)) {
  // Do something
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some这里读Array.prototype.some()

如果您的案例需要比较对象,也可以使用https://lodash.com/docs/#isEqual

编辑:

如果您比较mongodb ObjectId,则需要这样做https://stackoverflow.com/a/11638106/10216251ObjectId的原始结果不是纯字符串,因此您无法使用==

进行比较。