当前位置: 首页 > 知识库问答 >
问题:

在json数组集中,下一个索引的最小数量应大于上一个索引的最大数量

仲涵亮
2023-03-14

在这个 Json 数组中,我需要增加索引中的所有最小金额必须大于以前的索引最大金额,另一个条件是 maxamount 应该大于最小金额的相同索引

js prettyprint-override">// Code goes here

var app = angular.module('Demoapp',[]);
app.controller('ctrl',
function($scope){
$scope.model = [
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 0,
    "maxAmount": 20000,
    "minAmount": 0,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1837,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 15,
    "maxAmount": 500000,
    "minAmount": 20001,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1867,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 17,
    "maxAmount": 200000,
    "minAmount": 100001,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1873,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 20,
    "maxAmount": 999999999,
    "minAmount": 700002,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1879,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  }
];
$scope.volumeChange = function(data){
    angular.forEach($scope.model,function(o,index){
          if(o.minAmount === data.minAmount ){
            if($scope.model[index + 1]){
              $scope.model[index + 1].minAmount = data.maxAmount + 1
             
            }
          }
        })
}

});
<!DOCTYPE html>
<html>

  <head>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-app="Demoapp" ng-controller="ctrl">
      <div ng-repeat="data in model">
        <input class="text-right" type="number" ng-model="data.minAmount" name="minAmount"   min="0" max="999999998" ng-disabled="true" placeholder="from" />
        <input class="text-right" type="number" ng-change="volumeChange(data,volume_range)" ng-model="data.maxAmount" name="maxAmount"  min="{{data.minAmount+1}}" max="999999999" maxlength="9" id="maxAmount" placeholder="to" required>
        <input class="text-right" type="number" name="discount" id="discount" ng-model="data.discount" min="0" max="99.99" step="0.01" placeholder="00.00" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" required>
        
      </div>

</div>
  </body>

</html>

在上面的例子中,我已经根据更改最小金额更改了下一个索引值,我的例外输出是在更改单个最大金额的同时检查所有索引

共有1个答案

鄂昌胤
2023-03-14
// Code goes here


var app = angular.module('Demoapp',[]);
app.controller('ctrl',
function($scope){
$scope.model = [
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 0,
    "maxAmount": 20000,
    "minAmount": 0,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1837,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 15,
    "maxAmount": 500000,
    "minAmount": 20001,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1867,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 17,
    "maxAmount": 200000,
    "minAmount": 100001,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1873,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 20,
    "maxAmount": 999999999,
    "minAmount": 700002,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1879,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  }
];
$scope.volumeChange = function(data){
    angular.forEach($scope.model,function(o,index){
          if(o.minAmount === data.minAmount ){
            if($scope.model[index + 1]){
              $scope.model[index + 1].minAmount = data.maxAmount + 1
             
            }
          }
          if($scope.model[index + 1]){
            if($scope.model[index + 1].minAmount < $scope.model[index].maxAmount){
                $scope.model[index + 1].minAmount = $scope.model[index].maxAmount + 1
            }
          if($scope.model[index + 1].maxAmount < $scope.model[index + 1].minAmount){
             $scope.model[index + 1].maxAmount = $scope.model[index + 1].minAmount + 1
          }
        }
        })
}

});
<!DOCTYPE html>
<html>

  <head>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-app="Demoapp" ng-controller="ctrl">
      <div ng-repeat="data in model">
        <input class="text-right" type="number" ng-model="data.minAmount" name="minAmount"   min="0" max="999999998" ng-disabled="true" placeholder="from" />
        <input class="text-right" type="number" ng-change="volumeChange(data,volume_range)" ng-model="data.maxAmount" name="maxAmount"  min="{{data.minAmount+1}}" max="999999999" maxlength="9" id="maxAmount" placeholder="to" required>
        <input class="text-right" type="number" name="discount" id="discount" ng-model="data.discount" min="0" max="99.99" step="0.01" placeholder="00.00" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" required>
        
      </div>

</div>
  </body>

</html>
 类似资料:
  • 问题内容: 从如下所示的数组中,如何获取数组中最大值的索引。对于下面的数组,期望的结果将为‘11’。 问题答案: 我的解决方案是: 注意: 这样,您可以检索与给定 最大值 相关的 每个键 。 __ 如果您只对 其中一个键 感兴趣,只需使用 $ maxs [0]

  • 所以我在一次在线面试中被要求解决这个问题,但失败了。我立即被拒绝了。我正在试图找出我的算法出了什么问题。 两个最大的数字 用您选择的编程语言编写一个函数,该函数接受一个整数数组并返回前两个最大数字的索引。记录边缘情况下的任何特殊行为(如果有的话)。该函数的运行时间应为其中N是数组的长度和附加空间。 我编写了这个方法来对C#中的数字进行排序: 关于记录边缘案例的特殊行为,我得到了以下回应: 对于数字

  • 我知道在Swift 3中,循环的典型C风格发生了一些变化。我一直在努力解决这个问题,但在很多情况下,我似乎比以前编写了更多的代码。也许有人能把我引向正确的方向,因为这就是我想要的: 非常简单的东西。我希望能够获取我所在的索引,并且如果names.count==0,则不运行for循环。一气呵成。 但我在Swift 3中的选择似乎不允许我这样做。我必须做一些类似的事情: 需要在开始时使用if语句,因为

  • 问题内容: 我有一个二维NumPy数组。我知道如何获取轴上的最大值: 如何获得最大元素的索引?我想代替输出。 问题答案:

  • 给定一个非负整数数组,您最初位于数组的第一个索引处。 数组中的每个元素表示该位置的最大跳跃长度。 您的目标是以最少的跳跃次数达到最后一个索引。 例如:给定数组 到达最后一个指数的最小跳跃次数为2次。(从索引0跳转1步到1,然后3步到最后一个索引。) 我已经从左到右构建了一个dp[]数组,以便dp[i]指示从arr[0]到达arr[i]所需的最小跳转次数。最后,我们返回dp[n-1]。 我的代码的最

  • 我有两个数组 它们的长度都是4。City保存每个城市的名称,temp保存每个城市的平均温度。温度中的温度与城市中的城市顺序相同。所以我的问题是,我怎样才能得到temp中最大int的索引?我想打印出来