我有一个列表,
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
您可以使用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);