You need to enable JavaScript to run this app. 如何统计JSON数组中user_status为Active的用户总数量
优惠活动
大模型
产品
解决方案
定价
更多

如何统计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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 12:39:04