2017-11-14 1 views
0

나는 angularjs에서 프로젝트를 진행 중이다. 단일 값을 범위를 포함하는 json 데이터 배열에 매핑하고 값이 범위 내에있는 경우 반환하는 함수를 만드는 데 도움이 필요합니다. 즉 값이 범위 내에 있으면 해당 범위에 해당 반환 값을 할당합니다. 어떻게 내가 자바 스크립트에서 조회 기능을 사용하여 이것을 얻을 수 있습니다. 값> = 5.00 복귀 0이면상한선과 하한선 사이의 값을위한 자바 스크립트 룩업

[{"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.

함수를 리턴하는 경우 : I 사용 오전 JSON 테이블의 예는 이쪽 조회 할 json 테이블과 값을받은 다음 해당 간격에 해당하는 지점을 반환합니다. 사람이 유용 발견하면

+1

포스트 자바 스크립트 기능. –

+0

"value is> = 5.00 return 0"이 명령문의 값은 무엇입니까? –

+0

아직 어떤 기능도 만들지 않았습니다. 내가 자바 스크립트에서 조회 기능을 사용하여 몇 가지 검색을 해본 적이 있지만 대부분의 예제는 직접 매핑의 경우입니다. @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>

관련 문제