简体   繁体   English

Javascript查找上限和下限之间的值

[英]Javascript lookup for value between upper and lower limit

I am working on a project in angularjs. 我正在用angularjs开发一个项目。 I need help with creating a function which maps a single value to an array of json data containing range and return ie if value is within the range, then assign corresponding return value for that range. 我需要创建一个函数来将单个值映射到包含range并返回的json数据数组的帮助,例如,如果value在该范围内,则为该范围分配相应的返回值。 How can I achieve this using lookup function in javascript. 如何使用javascript中的查找功能实现此目的。 An example of a json table I am using is this: 我正在使用的json表的示例如下:

[{"LI":"05:0","LS":null,"points":0},
 {"LI":"04:00","LS":"05:00","points":3},
 {"LI":"03:00","LS":"4:00","points":4},
 {"LI":null,"LS":"3:00","points":4}
]

Basically, if value is >= 5.00 return 0, if value >= 4.00 and value < 5.00,, return 3. 基本上,如果值> = 5.00则返回0,如果值> = 4.00且值<5.00则返回3。

The function receives json table and value to lookup, then returns the points corresponding to that interval. 该函数接收json表和要查找的值,然后返回与该间隔相对应的点。

If anyone finds it useful, my final code is as follows: 如果有人发现它有用,我的最终代码如下:

 var app = angular.module('JsonMapping', []); app.controller('MainCtrl', function($scope) { $scope.Model = { Value: 10, Result: 0, ReferenceTable: [{"LI":85,"LS":null,"points":15},{"LI":84,"LS":85,"points":14},{"LI":83,"LS":84,"points":13},{"LI":82,"LS":83,"points":12},{"LI":81,"LS":82,"points":11},{"LI":80,"LS":81,"points":10},{"LI":78,"LS":80,"points":8},{"LI":76,"LS":78,"points":6},{"LI":74,"LS":76,"points":4},{"LI":72,"LS":74,"points":2},{"LI":70,"LS":72,"points":1},{"LI":null,"LS":70,"points":0}] } $scope.GetPointsForValue = function() { var lowerLimit = 0, upperLimit = 0, points = 0.0; for (var i = $scope.Model.ReferenceTable.length - 1; i >= 0; i--) { lowerLimit = $scope.Model.ReferenceTable[i].LI; upperLimit = $scope.Model.ReferenceTable[i].LS; if(lowerLimit === null && parseFloat($scope.Model.Value) < upperLimit) { $scope.Model.Result = $scope.Model.ReferenceTable[i].points; break; }else if (upperLimit === null && parseFloat($scope.Model.Value) >= lowerLimit) { $scope.Model.Result = $scope.Model.ReferenceTable[i].points; break; }else if (upperLimit > parseFloat($scope.Model.Value) && parseFloat($scope.Model.Value) >= lowerLimit){ $scope.Model.Result = $scope.Model.ReferenceTable[i].points; break; } } }; }); 
 <!DOCTYPE html> <html ng-app="JsonMapping"> <head> <meta charset="utf-8" /> <title>Map value to Json Array</title> <script> document.write('<base href="' + document.location + '" />'); </script> <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script> <script src="app.js"></script> </head> <body ng-controller="MainCtrl"> <input type="number" ng-model="Model.Value" ng-change="GetPointsForValue()"> <p>Points for value = {{Model.Result}} </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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