javascript - 这些重复的代码有没有办法简化下?js
本文介绍了javascript - 这些重复的代码有没有办法简化下?js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
$scope.ezzh_qsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'QSW';
});
$scope.ezzh_zsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'ZSW';
});
$scope.ezzh_hsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'HSW';
});
$scope.bsdw_qsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'QSW';
});
$scope.bsdw_zsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'ZSW';
});
$scope.bsdw_hsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'HSW';
});
$scope.bgdw_qsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'QSW';
});
$scope.bgdw_zsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'ZSW';
});
$scope.bgdw_hsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'HSW';
});
$scope.sgdw_qsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'QSW';
});
$scope.sgdw_zsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'ZSW';
});
$scope.sgdw_hsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'HSW';
});
$scope.bghs_qsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'QSW';
});
$scope.bghs_zsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'ZSW';
});
$scope.bghs_hsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'HSW';
});
$scope.sghs_qsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'QSW';
});
$scope.sghs_zsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'ZSW';
});
$scope.sghs_hsw = $scope.pls.filter(function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == 'HSW';
});
解决方案
var filter = function(text){
return function(item, index) {
return item.id.split('-')[1] == 'EZZH' && item.id.split('-')[2] == text;
}
}
// 使用
$scope.sghs_hsw = $scope.pls.filter(filter("HSW"))
还有更简洁的. 结合上面的:
var map = {
sghs_hsw:'HSW',
sghs_qsw :'QSW',
/// 省略其它的
}
_.each(map,function(v,k){
$scope[k] = $scope.pls.filter(filter(v))
})
这篇关于javascript - 这些重复的代码有没有办法简化下?js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文