JavaScript Splice from array works only in one order splice from array javascript,splice from array js,splice from

I have a little problem. I have a checkbox for showing div. I can choose from 6 different checkboxes to select which div I want to show.

When check checkboxes I show div and send to server checked name of 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

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

And if I uncheck in reverse order, first uncheck "D"

myArray = ["A", "C"]

second uncheck "C"

myArray = ["A"]

and last "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

myArray = ["A"]

if I first uncheck "A", my array remove "A" and "C" and "D", I get this

myArray = []

How to remove an only unchecked item?

Answer:1

You need to specify the deleteCount of Array#splice, because without all elements starting with the actual index are deleted.

Parameters

deleteCount Optional

An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. 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.

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.

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

I have the following g:submitButton: <g:submitButton name="next" class="signup-button skyblue-btn pull-right pl-pr-36" value="${message(code:"buttons.ok")}" onclick="return showSpinnerSignUp()"/&...

I have the following g:submitButton: <g:submitButton name="next" class="signup-button skyblue-btn pull-right pl-pr-36" value="${message(code:"buttons.ok")}" onclick="return showSpinnerSignUp()"/&...

  1. input type button onclick doesn't work

When importing a basic component I have webpack crash on me. I'm unsure as to why this is happening but it seems to be a missing lib within the ag-grid-react package... Has anyone run into this before?...

When importing a basic component I have webpack crash on me. I'm unsure as to why this is happening but it seems to be a missing lib within the ag-grid-react package... Has anyone run into this before?...

Am new to javascript i foud this one in w3schools.com - Entering pincode in search box will show in <p> tag !! How to do ? any help appreciated. Thanks in advance HTML : <form class="...

Am new to javascript i foud this one in w3schools.com - Entering pincode in search box will show in <p> tag !! How to do ? any help appreciated. Thanks in advance HTML : <form class="...

I'm trying to figure out how to compare if certain elements in two arrays compare in the same order. var compare = function (arr1, arr2) { //........ } compare ([f,t,r,m], [s,f,t,r,q,p,m]); /...

I'm trying to figure out how to compare if certain elements in two arrays compare in the same order. var compare = function (arr1, arr2) { //........ } compare ([f,t,r,m], [s,f,t,r,q,p,m]); /...