0

I am trying to compare the users choice (gender and country) to that of a json object(gender and country). If the comparison is true, then console.log the json's gender and country's "Value".

JS:

var app = angular.module('deathApp', []);
app.controller('data', function ($scope, $http) {
    $http.get("angular/death/data.json")
        .success(function (response) {

        $scope.ages = response.fact;
        //OBTAIN THEIR DEATH AGE
        //save their gender and country
        var gender = $('select[name=gender]').val();
        var country = $('select[name=country]').val();
        console.log("GENDER:" + gender + "." + "COUNTRY:" + country);
        //get their death age

        if (gender && country === gender and country from $scope.ages) {
            console.log(this.$scope.ages['Value'])
        }

json:

{
    "fact": [
        {
            "COUNTRY": "Afghanistan",
            "SEX": "Female",
            "Value": "62"
        },
        {
            "COUNTRY": "Afghanistan",
            "SEX": "Male",
            "Value": "61"
        },
        {
            "COUNTRY": "Albania",
            "SEX": "Female",
            "Value": "76"
        },
        {
            "COUNTRY": "Albania",
            "SEX": "Male",
            "Value": "73"
        },
        {
            "COUNTRY": "Algeria",
            "SEX": "Female",
            "Value": "74"
        },
        {
            "COUNTRY": "Algeria",
            "SEX": "Male",
            "Value": "70"
        }
    ]
}

Don't worry too much on the wiring of the json data, it is working fine. I can obtain the $scope.ages data fine.

5
  • You can't use «and». Use «&&» instead. Commented Nov 12, 2015 at 14:45
  • By "the wiring of the json data", do you mean how you're obtaining your gender and country values? If not, I think you should know that it's a bit peculiar to get them using jQuery when Angular has easier ways of tracking them. Is there a reason you're not using ngModel for them? Commented Nov 12, 2015 at 14:54
  • what's from in a if statement ? Commented Nov 12, 2015 at 15:03
  • @HarrisWeinstein I tried to use ngModel for that but got stuck and opted for jquery. Please feel free to show me how to do it with angular. I am using Angular to display the values on the front end, for the drop down options. I am using jquery to obtain the users input and storing them in a variable. Commented Nov 12, 2015 at 15:57
  • @artworkjpm Hey, I updated my answer to use your name for the object, and I included a plunker that also demonstrates how to use ngModel to automatically track the selected options. Commented Nov 12, 2015 at 16:29

3 Answers 3

1

You have to map to the data and also loop over your objects in your array. :

for(var i = 0; i < $scope.ages.length; i++){

 if (gender  === $scope.ages[i].SEX && country === $scope.ages[i].COUNTRY)
   {
      console.log($scope.ages[i].Value)
   }

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

1 Comment

very close but helped me a lot. I needed to change response.fact to $scope.ages.
1

Just use a for loop, and break when you've found what you're looking for.

for(var i=0; i<$scope.ages['fact'].length; i++)
    if(gender === $scope.ages['fact'][i]['SEX'] && country === $scope.ages['fact'][i]['COUNTRY']){
        console.log($scope.ages['fact'][i]['Value']);
        break;
}

Additionally, it looks like the array is sorted by country alphabetically. You can do a binary search on this to quickly get to the country you want, then decide between the two genders.

Here's a simple plunker that has this, as well as using ngModel to keep track of the sex and country selected: demo.

1 Comment

you have confused me with the use of 'yourJSO'. No variable name is holding the json data, it is called using angular and can be seen at the top. I guess I can use var yourJSO = $scope.ages ?
0

If you need to find a value from the given values, in json data, you could use the filter function, which works over arrays.

This code will return an array with an object from the given values.

$scope.age = $scope.ages.fact.filter(function(x) {
    return x.COUNTRY === country && x.SEX === gender;
})[0].Value; // From the current object only show the «Value» key. So finally, $scope.age will contain the age. Use [0] because, you will get an array with one object which match with the criteria search.

Something like this:

(function() {
  var app = angular.module("deathApp", []);

  app.controller("data", ["$scope",
    function($scope) {
      $scope.ages = {
        "fact": [{
          "COUNTRY": "Afghanistan",
          "SEX": "Female",
          "Value": "62"
        }, {
          "COUNTRY": "Afghanistan",
          "SEX": "Male",
          "Value": "61"
        }, {
          "COUNTRY": "Albania",
          "SEX": "Female",
          "Value": "76"
        }, {
          "COUNTRY": "Albania",
          "SEX": "Male",
          "Value": "73"
        }, {
          "COUNTRY": "Algeria",
          "SEX": "Female",
          "Value": "74"
        }, {
          "COUNTRY": "Algeria",
          "SEX": "Male",
          "Value": "70"
        }]
      };

      $scope.compare = function() {
        var gender = $('select[name=gender]').val();
        var country = $('select[name=country]').val();

        $scope.age = $scope.ages.fact.filter(function(x) {
          return x.COUNTRY === country && x.SEX === gender;
        })[0].Value;
      };
    }
  ]);
})();
<html data-ng-app="deathApp">

<head>
  <meta charset="utf-8" />
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

</head>

<body data-ng-controller="data">
  <select name="gender">
    <option value="Male">Male</option>
    <option value="Female">Female</option>
  </select>
  <select name="country">
    <option value="Albania">Albania</option>
    <option value="Afghanistan">Afghanistan</option>
  </select>
  <button data-ng-click="compare()">Compare</button>
  <br />Age: {{age}}
</body>

</html>

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.