javascript - Filter a JSON array based on a property
问题描述
I have a JSON array like this:
var records = [
{ Name: "Bob", EmployeeID: 1234, Status: "present" },
{ Name: "Jim", EmployeeID: 4432, Status: "present" },
{ Name: "Heather", EmployeeID: 4432, Status: "absent" },
]
And I want to filter the array list according to the status like(i.e if Status === "present"
).
[
{ "Name": "Bob", "EmployeeID": 1234, "Status": "present" },
{ "Name": "Jim", "EmployeeID": 4432, "Status": "present" }
]
解决方案
使用Array#filter仅获取present
状态记录。
该方法使用通过所提供函数实现的测试的所有元素
filter()
创建一个新元素。array
var records = [{
"Name": "Bob",
"EmployeeID": 1234,
"Status": "present"
},
{
"Name": "Jim",
"EmployeeID": 4432,
"Status": "present"
},
{
"Name": "Heather",
"EmployeeID": 4432,
"Status": "absent"
}
];
let preStatus = records.filter(({Status}) => Status === 'present')
console.log(preStatus)
推荐阅读
- database - 从 DB 获取数据后,对象属性为空。实体框架
- ios - 构建开源 iOS 应用程序时出现链接错误
- javascript - Adsense - 自定义后备
- ios-universal-links - 应用搜索 API 验证工具显示“example.com 正在返回 469。请检查您的网址并重试。”
- sql - 弹性搜索过滤器组合
- echo - Singularity exec - 回显重定向问题
- java - 如何拒绝 DML 和 DDL 查询?
- oauth - 报名后登记表
- python - 使用 python 脚本发送带有嵌入图像的 html 电子邮件
- javascript - 移动设备上的 BXSlider 4 全屏:如何保持原生垂直页面滚动(上、下)但保持水平触摸功能(左、右)?