gpt4 book ai didi

javascript - Angular $filter 用于在 JSON 响应内的数组中进行深度过滤

转载 作者:行者123 更新时间:2023-12-02 15:07:44 25 4
gpt4 key购买 nike

我想过滤对象结构有点复杂的对象。我正在寻找一种深度过滤,其中 Angular $filter 对数组内的元素起作用:

[
{
"schoolId": 12345,
"schoolName": "ZXCVB",
"classes": [
{
"classId": "C1",
"students": 50
},
{
"classId": "C2",
"students": 100
}
]
},

{
"schoolId": 98765,
"schoolName": "QWERTY",
"classes": [
{
"classId": "C1",
"students": 100
},
{
"classId": "C2",
"students": 50
}
]
}
]



(这是一系列学校。每所学校都有一系列类(class),并且类(class)都有 ID)
我在这里寻求帮助的是如何过滤 classId,以便它返回学校对象。

最佳答案

您应该使用 custom filter ,定义您自己的过滤函数。示例:

.filter('classFilter', [function () {
return function (input, classId) {
if(!classId) return input;
var out = [];
input.forEach(function(s) {
s.classes.forEach(function(c){
if(c.classId==classId) out.push(s);
})
});
return out;
};
}]);

直播: http://jsfiddle.net/87yod9bf/2/

关于javascript - Angular $filter 用于在 JSON 响应内的数组中进行深度过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35001082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com