我正在使用angularjs
,我在范围内有一个数组。我需要删除范围数组中No=1
大量数据的对象。我需要删除特定值称为'1'。
请帮助如何实现这一目标。
var data=[
{
"No":1,
"PatientState": "AK",
"DispenseMonth": "7/1/2016"
},
{
"No":2,
"PatientState": "AK",
"DispenseMonth": "8/1/2016"
},
{
"No":1,
"PatientState": "AK",
"DispenseMonth": "9/1/2016"
},
{
"No":1,
"PatientState": "AK",
"DispenseMonth": "10/1/2016"
},
{
"No":4,
"PatientState": "AK",
"DispenseMonth": "11/1/2016"
},
{
"No":1,
"PatientState": "AK",
"DispenseMonth": "2/1/2017"
},
{
"No":5,
"PatientState": "AK",
"DispenseMonth": "3/1/2017"
}
]
$scope.StateInformations =data;
答案 0 :(得分:5)
使用Array.filter
过滤您想要的内容并将过滤结果设置回$scope. StateInformations
<强> UPD:强>
根据您对我的回答的评论,我判断您可能需要自定义过滤器来获得您想要的内容,您也可以使用Array.filter
进入自定义过滤器。
请参阅以下代码段和 plunker demo 。
var app = angular.module("app", []);
app.controller("myCtrl", function($scope) {
$scope.conditions = [];
$scope.options = [{
check: false,
value: 1
}, {
check: false,
value: 2
}, {
check: false,
value: 3
}];
$scope.data = [{
"No": 1,
"PatientState": "AK",
"DispenseMonth": "7/1/2016"
},
{
"No": 2,
"PatientState": "AK",
"DispenseMonth": "8/1/2016"
},
{
"No": 1,
"PatientState": "AK",
"DispenseMonth": "9/1/2016"
},
{
"No": 1,
"PatientState": "AK",
"DispenseMonth": "10/1/2016"
},
{
"No": 4,
"PatientState": "AK",
"DispenseMonth": "11/1/2016"
},
{
"No": 1,
"PatientState": "AK",
"DispenseMonth": "2/1/2017"
},
{
"No": 5,
"PatientState": "AK",
"DispenseMonth": "3/1/2017"
}
];
$scope.setFilterCondition = function(option) {
if (option.checked) {
$scope.conditions.push(option.value);
} else {
$scope.conditions.splice($scope.conditions.indexOf(option.value), 1);
}
};
});
app.filter("sampleFilter", function() {
return function(input, condition) {
if (!input) { return []; }
if (!condition || condition.length === 0) { return input; }
return input.filter(function(item) {
for (var i = 0; i < condition.length; i++) {
if (item.No === condition[i]) {
return true;
}
}
return false;
});
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
<div ng-repeat="option in options">
<label>
<input type="checkbox" ng-model="option.checked" ng-change="setFilterCondition(option)">
{{option.value}}
</label>
</div>
<br>
<div ng-repeat="item in data | sampleFilter: conditions">
<span>{{item.No}}</span> -
<span>{{item.PatientState}}</span> -
<span>{{item.DispenseMonth}}</span>
</div>
</div>