tomersss2
tomersss2

Reputation: 155

Angular.js search for match object in array by 2 given properties

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

Upvotes: 1

Views: 1832

Answers (3)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27192

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>

Upvotes: 0

Manoj Lodhi
Manoj Lodhi

Reputation: 988

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 :)

Upvotes: 2

Anadi Sharma
Anadi Sharma

Reputation: 295

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!

Upvotes: 0

Related Questions