Javascript $仅按嵌套对象值筛选
我正在根据字符串数组过滤JSON数据。Javascript $仅按嵌套对象值筛选,javascript,arrays,angularjs,filter,Javascript,Arrays,Angularjs,Filter,我正在根据字符串数组过滤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/
$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组,我想通过点击复选框来过滤它。单击复选框后,我将复选框的值(将是标记
)推送到一个数组中
然后,我只想根据嵌套的标记值进行筛选
我的手表功能如下:
$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.$watchCollection('ActiveFilters',函数(newValue){
如果($scope.ActiveFilters.length>0){
$scope.FilteredData=$scope.Data;
对于(变量i=0;i<$scope.ActiveFilters.length;i++){
$scope.filteredata=$filter('filter')($scope.filteredata,$scope.ActiveFilters[i]);
log($scope.FilteredData);
//log($scope.ActiveFilters);
}
}
否则{
$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);
var项目=[{
“练习说明”:“前倾到椅子上,让手臂下垂,轻轻地左右摆动手臂”,
“练习ID”:“324”,
“练习名称”:“肩部摆动练习(内收/外展)”,
“图像”:[
1008,
1009,
1010
],
“标签”:[
“身体部位”,
“手臂”,
“上身”,
“设备”,
“主席”,
“软组织”
]
}];
var过滤器=功能(obj){
如果(obj['tags'].indexOf(“软组织”)!=-1{/我认为没有很好的理由使用$filter
,除非您试图在AngularJS标记/模板中进行过滤。只有在自定义指令中支持AngularJS过滤器表达式时,它才在JS中真正有用
下面是一个在vanilla JavaScript中执行标记过滤器的更完整示例:
var filterTags = ['foo', 'bar', 'baz'];
var incomingData = [ // simplified example
{id: 'banana', tags: ['foo', 'qux']},
{id: 'potato', tags: ['qux', 'baz', 'foo', 'bar']},
{id: 'carrot', tags: ['qux', 'quux']},
{id: 'cabbage', tags: ['foo', 'baz', 'bar']}
];
var dataMatchingAllTags = incomingData.filter(function (obj) {
return filterTags.every(function (tag) {
return obj.tags.indexOf(tag) !== -1;
});
}); // [{id: 'potato', …}, {id: 'cabbage', …}]
var dataMatchingAnyTag = incomingData.filter(function (obj) {
return filterTags.some(function (tag) {
return obj.tags.indexOf(tag) !== -1;
});
}); // [{id: 'banana', …}, {id: 'potato', …}, {id: 'cabbage', …}]
var dataMatchingTagsExactly = incomingData.filter(function (obj) {
return (
obj.tags.length === filterTags.length &&
filterTags.every(function (tag) {
return obj.tags.indexOf(tag) !== -1;
})
);
}); // [{id: 'cabbage'}]
在您的情况下,$scope.ActiveFilters
将是filterTags
,$scope.Data
将是incomingData
和$scope.filteredata
将是dataMatchingAllTags
,datamatchinganytags
或dataMatchingTagsExactly
,具体取决于您希望如何使用过滤器奥克
请注意,本例假设ES5,但考虑到AngularJS也假设ES5,我认为这不会是一个问题。理想情况下需要一个角度特定的答案。