gpt4 book ai didi

javascript - 将变量与 json 对象进行比较

转载 作者:行者123 更新时间:2023-12-02 15:26:17 25 4
gpt4 key购买 nike

我试图将用户的选择(性别和国家/地区)与 json 对象(性别和国家/地区)进行比较。如果比较为真,则 console.log json 的性别和国家/地区的“值”。

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"
}
]
}

不用太担心 json 数据的连接,它工作正常。我可以很好地获取 $scope.ages 数据。

最佳答案

您必须映射到数据并循环遍历数组中的对象。 :

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

}

关于javascript - 将变量与 json 对象进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com