
获取特定状态的 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 数据中的特定集合。










