【问题标题】:Incrementing numbers in an array in javascript在javascript中增加数组中的数字
【发布时间】:2020-07-04 05:43:09
【问题描述】:

我正在尝试向此visualization 添加功能,以增加或减少隐藏层中的节点。我添加了这段代码:

如前所述,为了增加或减少隐藏层中的隐藏层数。但我没有成功,因此,我需要一些帮助。谁能告诉我这是如何完成的,我在正确的轨道上吗?

for (var i = 0; i < vm.hiddenLayersDepths[hiddenLayerLoop]; i++) {
    function minus() {
        vm.hiddenLayersDepths.map(function(val){return --val;});
        //Arrays.fill(vm.hiddenLayersDepths, vm.hiddenLayersDepths[0] - 1);
    }

    function plus() {
        if (vm.hiddenLayersDepths[0] < 5) {
            vm.hiddenLayersDepths.map(function(val){return ++val;});
            //Arrays.fill(vm.hiddenLayersDepths, vm.hiddenLayersDepths[0] + 1);
        }
    }
}

编辑:我希望 + 或 - 独立用于每个隐藏层。

【问题讨论】:

    标签: javascript angularjs d3.js


    【解决方案1】:

    您可以通过事件绑定简单地做到这一点。将ng-click 与您的按钮元素一起使用,并在您的控制器中添加相应的方法。

    <input type="button" value="-" id="moins" ng-click="vm.decrementHiddenLayerCount()">
    <input type="button" value="+" id="plus" ng-click="vm.incrementHiddenLayerCount()">
    

    在您的控制器中添加相应的事件处理程序。在事件处理程序中,您只需要增加 ngModel 值。它将使您的视图和模型保持同步。我在下面添加了您需要添加事件处理程序的代码。

    app.controller('MainCtrl', function( $scope, $interval, $window, $q ) {
        var vm = this;
    
    vm.incrementHiddenLayerCount = function() {
      const hiddenValue = vm.hiddenLayerCountSlider.value;
      if (hiddenValue < vm.hiddenLayerCountSlider.options.ceil) {
        vm.hiddenLayerCountSlider.value += 1;
        vm.hiddenLayersCount = vm.hiddenLayerCountSlider.value;
        draw();
      }
      }
    
      vm.decrementHiddenLayerCount = function() {
        debugger;
        const hiddenValue = vm.hiddenLayerCountSlider.value;
        if (hiddenValue > vm.hiddenLayerCountSlider.options.floor) {
         vm.hiddenLayerCountSlider.value -= 1;
        vm.hiddenLayersCount = vm.hiddenLayerCountSlider.value;
        draw(); 
        }
      }
    

    请在此处找到工作代码:https://codepen.io/jasdeep7991/pen/mdJayKg

    【讨论】:

    • @Prine 非常感谢您的回答。但我不想添加新的隐藏层,而是想在隐藏层中添加节点。无论如何,我感谢你的帮助。非常感谢!
    【解决方案2】:

    Array.prototype.map()

    map() 方法创建一个新数组,其中填充了 在调用数组中的每个元素上调用提供的函数。

    因此,将值重新分配给 hiddenLayersDepths

    vm.minus = function() {
      vm.hiddenLayersDepths = vm.hiddenLayersDepths.map((i) => i - 1);
      draw();
    };
    
    vm.plus = function() {
      vm.hiddenLayersDepths = vm.hiddenLayersDepths.map((i) => i + 1);
      draw();
    };
    

    工作演示

    https://codepen.io/aswinkumar863/pen/ZEGVYBp

    【讨论】:

    • 感谢您的回答!这很酷,谢谢。但我希望能够分别从每个隐藏层添加/删除神经元。在您的示例中,+ 或 - 同时对应于所有隐藏层。能不能做到,这样我就可以分别控制每个隐藏层的神经元个数了?
    【解决方案3】:

    要专门从 ONE 层添加或删除节点,每个层都需要其 + 和 - 按钮。在为 hiddenLayersCount 中的每个索引生成这些单独的按钮时,我将从该组 + 和 - 按钮添加一个“数据集”连接到与其相关的图层。因此,在有两个隐藏层的情况下,在使用 js 添加按钮后,您最终会得到类似于 HTML 中的按钮:

    <div class"plus-minus-container">
      <input data-id="0" type="button" value="-" class="minus-button">
      <input data-id="0" type="button" value="+" class="plus-button">
    </div>
    
    <div class"plus-minus-container">
      <input data-id="1" type="button" value="-" class="minus-button">
      <input data-id="1" type="button" value="+" class="plus-button">
    </div>
    

    在使用 js 添加它们时,您可以为每个添加点击侦听器,在 vm 范围内(或您可以访问 vm.hiddenLayersDepths 数组的任何地方)调用 addNodeToHiddenLayer() 或 removeNodeFromHiddenLayer()。

    function addNodeToHiddenLayer(event){
      const layerIndex = parseInt(event.target.dataset.id,10);
    
      if (vm.hiddenLayersDepths[layerIndex] < 5) {
        vm.hiddenLayersDepths[layerIndex] += 1;
      }
    }
    
    function removeNodeToHiddenLayer(event){
      const layerIndex = parseInt(event.target.dataset.id,10);
      if (vm.hiddenLayersDepths[layerIndex] > 1) {
        vm.hiddenLayersDepths[layerIndex] -= 1;
      }
    }
    

    【讨论】:

    • 这看起来不错,但是当我尝试时它不起作用。您是否在我发布的 codepen 链接中尝试过这个?谢谢@PedroPessoa
    • @luftgekuhltlover,看看这支笔:codepen.io/ppessoa/pen/jOPdXwj 我不太了解 Angular,所以我在这个例子中只使用了标准 js,但如果这就是你正在寻找的功能,请告诉我为了。我在 js 和 html 块中添加了“NEW CODE STARTS HERE”cmets,以更明确地向您展示我所做的所有更改
    • 非常感谢!这正是我正在寻找的功能。另外,非常感谢 cmets 的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2013-05-22
    • 2017-07-03
    • 2021-10-10
    • 2011-12-15
    • 1970-01-01
    相关资源
    最近更新 更多