gpt4 book ai didi

javascript - $仅按嵌套对象值过滤

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

我正在根据字符串数组过滤 JSON 数据。 $scope.Data 的示例如下:

{
"exerciseDescription": "Lean forward onto a chair and allow the arm to hang down, gently swing the arm from side to side",
"exerciseID": "324",
"exerciseName": "Shoulder Pendular Exercise (Adduction/Abduction)",
"images": [
1008,
1009,
1010
],

"tags": [
"Body Part",
"Arm",
"Upper body",
"Equipment",
"Chair"
"Soft Tissue"
]
},

该数据总共有 4500 组,我想通过单击复选框来过滤它。单击复选框后,我将复选框的值(这将是一个标签)推送到一个数组。

然后我想仅过滤嵌套标签值。

我的 watch 功能在这里:

$scope.$watchCollection('ActiveFilters', function(newValue) {
if ($scope.ActiveFilters.length > 0) {
$scope.FilteredData = $scope.Data;
for (var i = 0; i < $scope.ActiveFilters.length; i++) {
$scope.FilteredData = $filter('filter')($scope.FilteredData, $scope.ActiveFilters[i]);
console.log($scope.FilteredData);
// console.log($scope.ActiveFilters);
}
}
else {
$scope.FilteredData = [];
}
});

因此,如果 $scope.FilteredData 在其嵌套标签数组中包含任何“ActiveFilters”,那么它将显示在范围中。

简而言之 - 我怎样才能只过滤嵌套标签数组。

最佳答案

在 JavaScript 中按数组中对象的值进行过滤:

var items = [{
"exerciseDescription": "Lean forward onto a chair and allow the arm to hang down, gently swing the arm from side to side",
"exerciseID": "324",
"exerciseName": "Shoulder Pendular Exercise (Adduction/Abduction)",
"images": [
1008,
1009,
1010
],

"tags": [
"Body Part",
"Arm",
"Upper body",
"Equipment",
"Chair",
"Soft Tissue"
]
}];

var filter = function (obj) {
if (obj['tags'].indexOf("Soft Tissue") != -1) { // <--- filter by value in tags array
return true;
}
return false;
}

var filtered = items.filter(filter);

我认为您会明白这个想法并将其调整为与 Angular 一起使用。

关于javascript - $仅按嵌套对象值过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29575544/

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