简体   繁体   中英

comparing variable to json object

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.

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 .

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

}

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM