我有一个带有对象的 JSON 数据库。每个属性都有一个特定的赋值:a、b 或 c。
[
{
"id": 1,
"category": "a"
},
{
"id": 2,
"category": "b"
},
{
"id": 3,
"category": "c"
},
{
"id": 4,
"category": "a"
},
{
"id": 5,
"category": "a"
},
{
"id": 5,
"category": "b"
}
]
Run Code Online (Sandbox Code Playgroud)
我想显示如下内容:
共有6 个项目: ax 3、 bx 2和 cx 1。
我知道我必须使用objectsinmyjsondatabase.length才能获得总数。
我想知道如何获得具有特定值的对象的长度(数量)?
定义一个函数:
getCount(character) {
return this.objects.filter(obj => obj.category === character).length;
}
Run Code Online (Sandbox Code Playgroud)
并将其称为:
this.getCount('a');