gpt4 book ai didi

javascript - 如何检查值属于 JSON 中的某个区间

转载 作者:行者123 更新时间:2023-12-03 04:10:10 25 4
gpt4 key购买 nike

我有一个输入字段

<md-input-container ng-if="gpa.grade_type==2"> 
<input type="text" ng-model="gpa.grade_percent" aria-label="Grade">
</md-input-container>

我想检查 gpa.grade_percent "percent_grade_from": 93 - "percent_grade_to": 96,JSON 中的值适合哪个区间这样我就必须在下一个字段中显示与间隔相关的比例。

<td>
<span ng-bind="gpa.grade_scale"></span>
</td>

JSON:

[
{
"id": 2,
"letter_grade": "A",
"percent_grade_from": 93,
"percent_grade_to": 96,
"scale": "4.0",
"honors_gpa_from": "4.3",
"honors_gpa_to": "4.4",
"ap_ib_gpa_from": "4.8",
"ap_ib_gpa_to": "4.9"
},
{
"id": 3,
"letter_grade": "A-",
"percent_grade_from": 90,
"percent_grade_to": 92,
"scale": "3.7",
"honors_gpa_from": "3.9",
"honors_gpa_to": "4.2",
"ap_ib_gpa_from": "4.4",
"ap_ib_gpa_to": "4.7"
},
{
"id": 4,
"letter_grade": "B+",
"percent_grade_from": 87,
"percent_grade_to": 89,
"scale": "3.3",
"honors_gpa_from": "3.6",
"honors_gpa_to": "3.8",
"ap_ib_gpa_from": "4.1",
"ap_ib_gpa_to": "4.3"
},
{
"id": 5,
"letter_grade": "B",
"percent_grade_from": 83,
"percent_grade_to": 86,
"scale": "3.0",
"honors_gpa_from": "3.3",
"honors_gpa_to": "3.5",
"ap_ib_gpa_from": "3.8",
"ap_ib_gpa_to": "4.0"
},
{
"id": 6,
"letter_grade": "B-",
"percent_grade_from": 80,
"percent_grade_to": 82,
"scale": "2.7",
"honors_gpa_from": "2.0",
"honors_gpa_to": "3.2",
"ap_ib_gpa_from": "3.4",
"ap_ib_gpa_to": "3.7"
},
{
"id": 7,
"letter_grade": "C+",
"percent_grade_from": 77,
"percent_grade_to": 79,
"scale": "2.3",
"honors_gpa_from": "2.6",
"honors_gpa_to": "2.8",
"ap_ib_gpa_from": "3.1",
"ap_ib_gpa_to": "3.3"
},
{
"id": 8,
"letter_grade": "C",
"percent_grade_from": 73,
"percent_grade_to": 76,
"scale": "2.0",
"honors_gpa_from": "2.3",
"honors_gpa_to": "2.5",
"ap_ib_gpa_from": "2.8",
"ap_ib_gpa_to": "3.0"
},
{
"id": 9,
"letter_grade": "C-",
"percent_grade_from": 70,
"percent_grade_to": 72,
"scale": "1.7",
"honors_gpa_from": "1.9",
"honors_gpa_to": "2.2",
"ap_ib_gpa_from": "2.4",
"ap_ib_gpa_to": "2.7"
},
{
"id": 10,
"letter_grade": "D+",
"percent_grade_from": 67,
"percent_grade_to": 69,
"scale": "1.3",
"honors_gpa_from": "1.6",
"honors_gpa_to": "1.8",
"ap_ib_gpa_from": "2.1",
"ap_ib_gpa_to": "2.3"
},
{
"id": 11,
"letter_grade": "D",
"percent_grade_from": 65,
"percent_grade_to": 66,
"scale": "1.0",
"honors_gpa_from": "0.1",
"honors_gpa_to": "1.5",
"ap_ib_gpa_from": "0.1",
"ap_ib_gpa_to": "2.0"
},
{
"id": 12,
"letter_grade": "E/F",
"percent_grade_from": 0,
"percent_grade_to": 65,
"scale": "0.0",
"honors_gpa_from": "0.0",
"honors_gpa_to": null,
"ap_ib_gpa_from": "0.0",
"ap_ib_gpa_to": null
},
{
"id": 1,
"letter_grade": "A+",
"percent_grade_from": 97,
"percent_grade_to": 100,
"scale": "4.0",
"honors_gpa_from": "4.5",
"honors_gpa_to": "4.5",
"ap_ib_gpa_from": "5.0",
"ap_ib_gpa_to": "5.0"
}
]

最佳答案

angular.module('app', []).controller('MyController', ['$scope', function($scope) {
$scope.gpa = {grade_percent:68};

var data = [
{
"id": 2,
"letter_grade": "A",
"percent_grade_from": 93,
"percent_grade_to": 96,
"scale": "4.0",
"honors_gpa_from": "4.3",
"honors_gpa_to": "4.4",
"ap_ib_gpa_from": "4.8",
"ap_ib_gpa_to": "4.9"
},
{
"id": 3,
"letter_grade": "A-",
"percent_grade_from": 90,
"percent_grade_to": 92,
"scale": "3.7",
"honors_gpa_from": "3.9",
"honors_gpa_to": "4.2",
"ap_ib_gpa_from": "4.4",
"ap_ib_gpa_to": "4.7"
},
{
"id": 4,
"letter_grade": "B+",
"percent_grade_from": 87,
"percent_grade_to": 89,
"scale": "3.3",
"honors_gpa_from": "3.6",
"honors_gpa_to": "3.8",
"ap_ib_gpa_from": "4.1",
"ap_ib_gpa_to": "4.3"
},
{
"id": 5,
"letter_grade": "B",
"percent_grade_from": 83,
"percent_grade_to": 86,
"scale": "3.0",
"honors_gpa_from": "3.3",
"honors_gpa_to": "3.5",
"ap_ib_gpa_from": "3.8",
"ap_ib_gpa_to": "4.0"
},
{
"id": 6,
"letter_grade": "B-",
"percent_grade_from": 80,
"percent_grade_to": 82,
"scale": "2.7",
"honors_gpa_from": "2.0",
"honors_gpa_to": "3.2",
"ap_ib_gpa_from": "3.4",
"ap_ib_gpa_to": "3.7"
},
{
"id": 7,
"letter_grade": "C+",
"percent_grade_from": 77,
"percent_grade_to": 79,
"scale": "2.3",
"honors_gpa_from": "2.6",
"honors_gpa_to": "2.8",
"ap_ib_gpa_from": "3.1",
"ap_ib_gpa_to": "3.3"
},
{
"id": 8,
"letter_grade": "C",
"percent_grade_from": 73,
"percent_grade_to": 76,
"scale": "2.0",
"honors_gpa_from": "2.3",
"honors_gpa_to": "2.5",
"ap_ib_gpa_from": "2.8",
"ap_ib_gpa_to": "3.0"
},
{
"id": 9,
"letter_grade": "C-",
"percent_grade_from": 70,
"percent_grade_to": 72,
"scale": "1.7",
"honors_gpa_from": "1.9",
"honors_gpa_to": "2.2",
"ap_ib_gpa_from": "2.4",
"ap_ib_gpa_to": "2.7"
},
{
"id": 10,
"letter_grade": "D+",
"percent_grade_from": 67,
"percent_grade_to": 69,
"scale": "1.3",
"honors_gpa_from": "1.6",
"honors_gpa_to": "1.8",
"ap_ib_gpa_from": "2.1",
"ap_ib_gpa_to": "2.3"
},
{
"id": 11,
"letter_grade": "D",
"percent_grade_from": 65,
"percent_grade_to": 66,
"scale": "1.0",
"honors_gpa_from": "0.1",
"honors_gpa_to": "1.5",
"ap_ib_gpa_from": "0.1",
"ap_ib_gpa_to": "2.0"
},
{
"id": 12,
"letter_grade": "E/F",
"percent_grade_from": 0,
"percent_grade_to": 65,
"scale": "0.0",
"honors_gpa_from": "0.0",
"honors_gpa_to": null,
"ap_ib_gpa_from": "0.0",
"ap_ib_gpa_to": null
},
{
"id": 1,
"letter_grade": "A+",
"percent_grade_from": 97,
"percent_grade_to": 100,
"scale": "4.0",
"honors_gpa_from": "4.5",
"honors_gpa_to": "4.5",
"ap_ib_gpa_from": "5.0",
"ap_ib_gpa_to": "5.0"
}
];
$scope.find = function(){
$scope.gpa.grade_scale = (data.filter(function(x){
return x.percent_grade_from <= $scope.gpa.grade_percent && x.percent_grade_to >= $scope.gpa.grade_percent;
})[0] || {}).scale;
}
$scope.find();
}])
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>

<div ng-app='app' ng-controller="MyController">
<input type="text" ng-model="gpa.grade_percent" aria-label="Grade" ng-change='find()'>
<br/>
<span ng-bind='gpa.grade_scale'></span>
</div>

关于javascript - 如何检查值属于 JSON 中的某个区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44363581/

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