1

i have a table of CItyDistance: CIty1Id|City2Id|Distance(KM), Now in my project i receive 2 cities Id's, i want to check if there is a calculated distance for these 2 cities.

so it doesn't matter who will be City1 Or City2 , i need to check both options. The way that i found to check it is too long and messy. Can anyone offer an alternative? (Please check Plunker Example: 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); 
1
  • Check my answer it will work as per your expectation. Commented Feb 8, 2017 at 18:28

3 Answers 3

2

You can try this, Replace you $scope.findDistance function with this. I think it has less code and efficient way to achieve your requirement.

 $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;
        }
    });
};

Added plunker, https://plnkr.co/edit/3r3intufeiqc26kzcnca?p=preview

Thanks, Best of luck :)

Sign up to request clarification or add additional context in comments.

2 Comments

Nice, i tried using IndexOf but with no success, well it's a nice way, if there will be no better way suggested i'll use this one, Thanks
Well, your code has a flaw, what if the distance is similar to 1 of the Id's u r looking for? it can be solved by: arr.splice(2, 1). but thanks, i'll accept this answer as it is the shortest one
0

I think what you have implemented is fine because of the json structure. All I can suggest is the code below

$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;
      }
    }
};

Please let me know if it helps!

2 Comments

that's more messy than above :)
Yeah, but is more efficient than above when you have a large set of data.
0

Use below method & operator to optimize your code :

  • Use Array filter() method to iterate the array and creates a new array with all elements that pass the test implemented by the provided function.
  • Use JavaScript ternary operator This operator is frequently used as a shortcut for the if statement.

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.