[英]Count number of items with active state
我有一個功能可以在單擊的列表項上切換活動狀態:
Vue
toggleActive: function(s){
s.active = !s.active;
},
哈巴狗
li(v-for='property in properties', v-on:click='toggleActive(property)'
以后如何檢查處於活動狀態的項目數? 理想情況下,我想將一個變量設置為活動狀態項的數量。
我最初想到的是這樣的:
var count = this.properties.active.length;
但是無法正常工作。
在數組/對象上使用ES6篩選器:
this.properties.filter(value => value.active === true).length;
您可以將其縮短為:
this.properties.filter(v => v.active).length
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.