简体   繁体   English

阵列拼接只能按一个顺序进行

[英]Splice from array works only in one order

I have a little problem. 我有一点问题。 I have a checkbox for showing div. 我有一个用于显示div的复选框。 I can choose from 6 different checkboxes to select which div I want to show. 我可以从6个不同的复选框中进行选择,以选择要显示的div。

When check checkboxes I show div and send to server checked name of div. 选中复选框时,我显示div并发送到服务器的div选中名称。

 $scope.savesGraphUserDetail = [];
    $scope.addRemoveUserChart = function(checked, value) {
      if (checked) {
        $scope.savesGraphUserDetail.push(value);

        var config = { headers: { "Content-Type": "application/x-www-form-urlencoded", "X-HTTP-Method-Override": "PUT" } };
        var data = {graphsConfig: $scope.savesGraphUserDetail};

        $http
        .post(serviceBase + "blabla", data, config)
          .success(function(data, status, headers, config) {
          })
          .error(function(data, status, header, config) {
          });
      }else {
        var index = $scope.savesGraphUserDetail.indexOf(value);
        $scope.savesGraphUserDetail.splice(index);

        var config = { headers: { "Content-Type": "application/x-www-form-urlencoded", "X-HTTP-Method-Override": "PUT" } };
        var data = {graphsConfig: $scope.savesGraphUserDetail};

        $http
          .post(serviceBase + "blabla", data, config)
          .success(function(data, status, headers, config) {
          })
          .error(function(data, status, header, config) {
          });
      }

And this is ok, but when I uncheck checkboxes I get a problem, because if I check in this order first click "A", second "C" and third "D" I push in my array like this 没关系,但是当我取消选中复选框时会遇到问题,因为如果我按此顺序进行检查,请首先单击“ A”,第二个“ C”和第三个“ D”,这样将数组推入

myArray = ["A", "C", "D"]

And if I uncheck in reverse order, first uncheck "D" 如果我以相反的顺序取消选中,请先取消选中“ D”

myArray = ["A", "C"]

second uncheck "C" 第二次取消选中“ C”

myArray = ["A"]

and last "A" 最后一个“ A”

myArray = []

and this is perfect, but if I change order when unchecking checkboxes 这是完美的,但是如果我取消选中复选框时更改顺序

myArray = ["A", "C", "D"]

if I first uncheck "C", my array remove "C" and "D", i get this 如果我先取消选中“ C”,我的数组删除“ C”和“ D”,我得到这个

myArray = ["A"]

if I first uncheck "A", my array remove "A" and "C" and "D", I get this 如果我先取消选中“ A”,我的数组删除“ A”和“ C”和“ D”,我得到这个

myArray = []

How to remove an only unchecked item? 如何删除唯一未选中的项目?

You need to specify the deleteCount of Array#splice , because without all elements starting with the actual index are deleted. 您需要指定Array#splicedeleteCount ,因为没有删除所有以实际索引开头的元素。

Parameters 参数

deleteCount Optional deleteCount 可选

An integer indicating the number of old array elements to remove. 一个整数,指示要删除的旧数组元素的数量。 If deleteCount is 0, no elements are removed. 如果deleteCount为0,则不会删除任何元素。 In this case, you should specify at least one new element. 在这种情况下,您应该至少指定一个新元素。 If deleteCount is greater than the number of elements left in the array starting at start , then all of the elements through the end of the array will be deleted. 如果deleteCount大于从start的数组中剩余的元素数,则将删除数组末尾的所有元素。

If deleteCount is omitted, or if its value is larger than array.length - start , then all of the elements beginning with start index on through the end of the array will be deleted. 如果省略deleteCount ,或者其值大于array.length - start ,则将从start index start直到数组结尾的所有元素都将被删除。

$scope.savesGraphUserDetail.splice(index, 1);
//                                        ^

Your problem is that you are calling .splice() method with the index only, you need to sepecify 1 as second parameter, which is the number of items to be spliced, the deleteCount optional param. 您的问题是您仅使用index调用.splice()方法 ,需要将1分隔为第二个参数,这是要拼接的项目数,即deleteCount可选参数。

var index = $scope.savesGraphUserDetail.indexOf(value);
$scope.savesGraphUserDetail.splice(index, 1);

Otherwise all of the elements beginning with start index on through the end of the array will be deleted, that's why "C" and "D" were deleted in your second try. 否则,将从开始索引到数组末尾的所有元素都将被删除,这就是为什么在第二次尝试中删除"C""D"的原因。

Documentation: 文档:

If you take a look at the .splice() MDN specification , you can see that: 如果查看.splice() MDN规范 ,可以看到:

在此处输入图片说明

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

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