如何使用angularjs基于奇偶值拼接数组 [英] how to splice the array based on paritcular value using angularjs
问题描述
我正在使用 angularjs
,并且在作用域中有一个数组.而且,我需要删除范围数组中有大量数据的 No = 1
处的对象.我需要删除特定的值称为"1".
请帮助实现该目标.
var data = [{否":1,"PatientState":"AK","DispenseMonth":"2016年7月1日"},{否":2,"PatientState":"AK","DispenseMonth":"2016年8月1日"},{否":1,"PatientState":"AK","DispenseMonth":"9/1/2016"},{否":1,"PatientState":"AK","DispenseMonth":"2016年10月1日"},{否":4,"PatientState":"AK","DispenseMonth":"11/1/2016"},{否":1,"PatientState":"AK","DispenseMonth":"2/1/2017"},{否":5,"PatientState":"AK","DispenseMonth":"2017年3月1日"}]$ scope.StateInformations =数据;
使用 Array.filter
过滤所需内容,并将过滤结果设置回 $ scope.StateInformations
UPD:
根据您对我的答案的评论,我判断您可能需要一个自定义过滤器才能获得所需的内容,也可以使用 Array.filter
进入自定义过滤器.
请参考以下代码段以及此 plunker演示 ..>
var app = angular.module("app",[]);app.controller("myCtrl",function($ scope){$ scope.conditions = [];$ scope.options = [{检查:错误,价值:1},{检查:错误,价值:2},{检查:错误,价值:3}];$ scope.data = [{否":1,"PatientState":"AK","DispenseMonth":"2016年7月1日"},{否":2"PatientState":"AK","DispenseMonth":"2016年8月1日"},{否":1,"PatientState":"AK","DispenseMonth":"9/1/2016"},{否":1,"PatientState":"AK","DispenseMonth":"2016年10月1日"},{否":4"PatientState":"AK","DispenseMonth":"11/1/2016"},{否":1,"PatientState":"AK","DispenseMonth":"2/1/2017"},{否":5"PatientState":"AK","DispenseMonth":"2017年3月1日"}];$ scope.setFilterCondition = function(option){如果(option.checked){$ scope.conditions.push(option.value);} 别的 {$ scope.conditions.splice($ scope.conditions.indexOf(option.value),1);}};});app.filter("sampleFilter",function(){返回函数(输入,条件){如果(!input){return [];}if(!condition || condition.length === 0){返回输入;}返回input.filter(function(item){for(var i = 0; i< condition.length; i ++){if(item.No === condition [i]){返回true;}}返回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 =选项中的选项"><标签>< input type ="checkbox" ng-model ="option.checked" ng-change ="setFilterCondition(option)">{{option.value}}</label></div>< br>< div ng-repeat =数据中的项目| sampleFilter:条件">< span> {{item.No}}}</span>--< span> {{item.PatientState}}</span>--< span> {{item.DispenseMonth}}</span></div></div>
I'm using angularjs
and I have an array in scope. And I need to remove objects where No=1
in scope array lots of data in there. I need to remove particular value is called '1'.
Please help how to achieve this.
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;
use Array.filter
to filter what you want and set filter result back to $scope. StateInformations
UPD:
according your comments on my answer, I judged you may need a custom filter to get what you want, you can use Array.filter
into custom filter also.
refer the below code snippet and this 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>
这篇关于如何使用angularjs基于奇偶值拼接数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!