簡體   English   中英

如何在數組derbyjs上使用indexof

[英]How to use indexof on array derbyjs

我有一個數組傳遞給我的模板,我想看看是否有存儲在其中的值:

{{each _page.friends as #friend}}
    {{if _page.user.friends.indexOf(#friend.id)<0}}
        <button>Add</button>
    {{else}}
        Already Friends
    {{/if}}
{{/each}}

顯然indexOf不是一個函數,但是數組(_page.user.friends)確實存在,我可以使用它自己的{{each}} ....

有沒有辦法做到這一點? 還是更好的方法?

我沒有在Derby View文檔中看到對indexOf支持。 但是,您始終可以使用查看功能來確定某人是否是朋友。

// in the view
{{each _page.friends as #friend}}
  {{if isFriend(_page.user.friends, #friend.id)}}
     <button>Add</button>
  {{else}}
    Already Friends
  {{/if}}
{{/each}}

// in controller
FriendListController.prototype.isFriend = function(friends, friendId) {
  return friends.indexOf(friendId) > 0;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM