如何统计JSON数组中user_status为Active的用户总数量
实现代码
你可以直接对用户列表数组调用filter方法筛选状态为Active的用户,再取筛选结果的长度即可得到总数:
// 方式1:先筛选再统计,可保留筛选后的用户列表 const activeUserList = this.user.body.existing_users.filter(item => item.user_status === 'Active') const activeUserTotal = activeUserList.length console.log('Active状态用户总数:', activeUserTotal) // 方式2:仅需要总数时可以简写为一行 const activeUserTotal = this.user.body.existing_users.filter(item => item.user_status === 'Active').length
逻辑说明
Array.filter()方法会遍历原数组,返回所有符合判断条件的元素组成的新数组,这里我们的判断条件就是元素的user_status字段严格等于Active- 筛选完成后,新数组的
length属性就是符合要求的用户总数
内容的提问来源于stack exchange,提问作者Santhosh
相关产品推荐
相关产品推荐

