
获取特定状态的 json 集合
假设我们有一个 json 数据集,其中包含不同人员的信息:
[
{ "id": 1, "name": "alice", "age": [{ "id": 4, "status": 0 }, { "id": 4, "status": 1 }] },
{ "id": 2, "name": "bob", "age": [{ "id": 4, "status": 1 }] },
{ "id": 3, "name": "charlie", "age": [{ "id": 4, "status": 1 }] }
]获取 status 为 0 的集合
要获取 status 为 0 的年龄集合,我们可以使用 javascript 的 filter 方法:
const data = [
{ "id": 1, "name": "alice", "age": [{ "id": 4, "status": 0 }, { "id": 4, "status": 1 }] },
{ "id": 2, "name": "bob", "age": [{ "id": 4, "status": 1 }] },
{ "id": 3, "name": "charlie", "age": [{ "id": 4, "status": 1 }] }
];
const status0 = data.filter(item => item.age.some(a => a.status === 0));
console.log(status0);这样会输出:
[
{ "id": 1, "name": "alice", "age": [{ "id": 4, "status": 0 }] }
]获取 status 为 1 的集合
我们可以使用类似的方法获取 status 为 1 的年龄集合:
const status1 = data.filter(item => item.age.some(a => a.status === 1)); console.log(status1);
这样会输出:
[
{ "id": 1, "name": "Alice", "age": [{ "id": 4, "status": 1 }] },
{ "id": 2, "name": "Bob", "age": [{ "id": 4, "status": 1 }] },
{ "id": 3, "name": "Charlie", "age": [{ "id": 4, "status": 1 }] }
]通过使用 filter 方法,我们可以根据指定的条件轻松地提取 json 数据中的特定集合。
以上就是如何从包含状态信息的 JSON 数据集中获取特定状态的集合?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号