Angular.js 通过 2 个给定属性在数组中搜索匹配对象
Angular.js search for match object in array by 2 given properties
我有 table 的 CItyDistance:CIty1Id|City2Id|Distance(KM),
现在在我的项目中我收到了 2 个城市的 ID,我想检查是否有计算这 2 个城市的距离。
所以无论谁是 City1 或 City2 都无关紧要,我需要检查这两个选项。
我发现检查它的方式太长而且混乱。
谁能提供替代方案?
(请检查 Plunker 示例:https://plnkr.co/edit/nzB8zy0034LqJFgL8qk7?p=preview
$scope.CompanyCity = { Id: 59, Name: 'berline' };
$scope.result = "";
$scope.distances = [
{city1: 59,city2: 1, Distance: 50 },
{city1: 1, city2: 58, Distance: 80 },
{city1: 3, city2: 59, Distance: 25 },
{city1: 4, city2: 1, Distance: 120 }];
$scope.findDistance = function(studentCityID) {
angular.forEach($scope.distances, function(value, key) {
if (value.city1 == studentCityID && value.city2 == $scope.CompanyCity.Id) {
$scope.result = value.Distance;
}
else if (value.city2 == studentCityID && value.city1 == $scope.CompanyCity.Id) {
$scope.result = value.Distance;
}
});
};
$scope.findDistance(1);
由于 json 结构,我认为您实现的很好。我只能建议下面的代码
$scope.findDistance = function(studentCityID) {
for (var count=0; count<$scope.distances.length; count++) {
if (($scope.distances[count].city1 == studentCityID && $scope.distances[count].city2 == $scope.CompanyCity.Id) ||
($scope.distances[count].city2 == studentCityID && $scope.distances[count].city1 == $scope.CompanyCity.Id)) {
$scope.result = $scope.distances[count].Distance;
break;
}
}
};
如果有帮助请告诉我!
你可以试试这个,把你的$scope.findDistance
函数换成这个。我认为它有更少的代码和有效的方式来满足您的要求。
$scope.findDistance = function(studentCityID) {
angular.forEach($scope.distances, function(value, key) {
var arr = Object.values(value);
if(arr.indexOf(studentCityID) !== -1 && arr.indexOf($scope.CompanyCity.Id) !== -1) {
$scope.result = value.Distance;
}
});
};
添加了 plunker,https://plnkr.co/edit/3r3intufeiqc26kzcnca?p=preview
谢谢,祝你好运:)
使用下面的方法和运算符来优化您的代码:
- 使用 Array filter() 方法迭代数组并创建一个新数组,其中包含通过提供的函数实现的测试的所有元素。
- 使用 JavaScript ternary operator 此运算符经常用作 if 语句的快捷方式。
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope) {
$scope.CompanyCity = { Id: 59, Name: 'berline' };
$scope.distances = [
{city1: 59,city2: 1, Distance: 50 },
{city1: 1, city2: 58, Distance: 80 },
{city1: 3, city2: 59, Distance: 25 },
{city1: 4, city2: 1, Distance: 120 }
];
$scope.findDistance = function(studentCityID) {
var res = $scope.distances.filter(function(item) {
return (item.city1 == studentCityID && item.city2 == $scope.CompanyCity.Id) ? item.Distance : ((item.city2 == studentCityID && item.city1 == $scope.CompanyCity.Id) ? item.Distance : '');
});
console.log(res[0].Distance); // Distance
};
$scope.findDistance(1);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
</div>
我有 table 的 CItyDistance:CIty1Id|City2Id|Distance(KM), 现在在我的项目中我收到了 2 个城市的 ID,我想检查是否有计算这 2 个城市的距离。
所以无论谁是 City1 或 City2 都无关紧要,我需要检查这两个选项。 我发现检查它的方式太长而且混乱。 谁能提供替代方案? (请检查 Plunker 示例:https://plnkr.co/edit/nzB8zy0034LqJFgL8qk7?p=preview
$scope.CompanyCity = { Id: 59, Name: 'berline' };
$scope.result = "";
$scope.distances = [
{city1: 59,city2: 1, Distance: 50 },
{city1: 1, city2: 58, Distance: 80 },
{city1: 3, city2: 59, Distance: 25 },
{city1: 4, city2: 1, Distance: 120 }];
$scope.findDistance = function(studentCityID) {
angular.forEach($scope.distances, function(value, key) {
if (value.city1 == studentCityID && value.city2 == $scope.CompanyCity.Id) {
$scope.result = value.Distance;
}
else if (value.city2 == studentCityID && value.city1 == $scope.CompanyCity.Id) {
$scope.result = value.Distance;
}
});
};
$scope.findDistance(1);
由于 json 结构,我认为您实现的很好。我只能建议下面的代码
$scope.findDistance = function(studentCityID) {
for (var count=0; count<$scope.distances.length; count++) {
if (($scope.distances[count].city1 == studentCityID && $scope.distances[count].city2 == $scope.CompanyCity.Id) ||
($scope.distances[count].city2 == studentCityID && $scope.distances[count].city1 == $scope.CompanyCity.Id)) {
$scope.result = $scope.distances[count].Distance;
break;
}
}
};
如果有帮助请告诉我!
你可以试试这个,把你的$scope.findDistance
函数换成这个。我认为它有更少的代码和有效的方式来满足您的要求。
$scope.findDistance = function(studentCityID) {
angular.forEach($scope.distances, function(value, key) {
var arr = Object.values(value);
if(arr.indexOf(studentCityID) !== -1 && arr.indexOf($scope.CompanyCity.Id) !== -1) {
$scope.result = value.Distance;
}
});
};
添加了 plunker,https://plnkr.co/edit/3r3intufeiqc26kzcnca?p=preview
谢谢,祝你好运:)
使用下面的方法和运算符来优化您的代码:
- 使用 Array filter() 方法迭代数组并创建一个新数组,其中包含通过提供的函数实现的测试的所有元素。
- 使用 JavaScript ternary operator 此运算符经常用作 if 语句的快捷方式。
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope) {
$scope.CompanyCity = { Id: 59, Name: 'berline' };
$scope.distances = [
{city1: 59,city2: 1, Distance: 50 },
{city1: 1, city2: 58, Distance: 80 },
{city1: 3, city2: 59, Distance: 25 },
{city1: 4, city2: 1, Distance: 120 }
];
$scope.findDistance = function(studentCityID) {
var res = $scope.distances.filter(function(item) {
return (item.city1 == studentCityID && item.city2 == $scope.CompanyCity.Id) ? item.Distance : ((item.city2 == studentCityID && item.city1 == $scope.CompanyCity.Id) ? item.Distance : '');
});
console.log(res[0].Distance); // Distance
};
$scope.findDistance(1);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
</div>