如何计算列表的长度已经阅读为真(How to count length of list having

2019-10-28 12:03发布

我有一个列表,

let chats: Chats[] = [
{id: 1, name: "James Anderson",read=false},
{id: 2, name: "James Anderson",read=true},
{id: 3, name: "James Anderson",read=true},
{id: 4, name: "James Anderson",read=true},
]

和CSHTML页我用权{{chats.length}}它返回所有计数,但我需要返回其读取值设置为假计数

即我需要的数量为1,而不是4

Answer 1:

您可以使用array.filter和长度如下,

let result  = chats.filter(t=>!t.read).length;

DEMO

 var chats = [ {id: 1, name: "James Anderson",read:false}, {id: 2, name: "James Anderson",read:true}, {id: 3, name: "James Anderson",read:true}, {id: 4, name: "James Anderson",read:true} ]; let result = chats.filter(t=>!t.read).length; console.log(result); 



文章来源: How to count length of list having read as true