2017-11-14 8 views
0

私はangularjsのプロジェクトに取り組んでいます。範囲を含むjsonデータの配列に単一の値をマップし、戻り値が範囲内にあれば、その範囲に対応する戻り値を代入する関数を作成するのに助けが必要です。どのように私はjavascriptでルックアップ関数を使用してこれを達成することができます。値が> = 5.00リターン0であれば上限と下限の間の値のJavascriptルックアップ

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

基本的には、値が> = 4.00と値< 5.00 ,, 3

機能を返す場合:私は使用していますJSONテーブルの例がこれですルックアップするjsonテーブルと値を受け取り、その間隔に対応するポイントを返します。誰もがそれを有用見つけた場合

+1

ポストあなたのJavaScript関数。 –

+0

"value is> = 5.00 return 0"この文の値は何ですか? –

+0

私はまだすべての機能を作成していません。私はjavascriptで検索関数を使用していくつかの検索を行っているが、ほとんどの例は直接マッピングの場合です。 @Rupinder、例:上記の配列でFunctionName(value、jsonArray)のようなプロトタイプを使用している場合、FunctionName(3.5、jsonArray)を実行すると結果は4になります。 –

答えて

0

は、私の最終的なコードは次のとおりです。あなたの質問で

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="[email protected]" 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>

関連する問題