0

Lets say I have this variable

  this.mylist.push([{key:{"test1":name,"test":desc,"hi i am a test":other,"none":type,"amount":0} }]);

This will be added to the array mylist I want however on some condition to remove the current created list by deleting it through the unique key so

I want to splice the object array so the value will be removed to prevent duplicates

I tried

this.mylist.splice(this.mylist.indexOf(key), 1);

But did not work i try to remove the array by getting the unique key which holds these child values

I also tried

      this.mylist.splice(this.mylist.indexOf([{key}]), 1);

Someone who can help me out :(

      CheckBox(values,values,values) {

        this.bool = (this.mylist.indexOf(key) === -1);


        if (this.bool) { 
          this.mylist.push([{key:{"key":key,"naam":naam,"beschrijving":beschrijving,"type":type,"aantal":aantal} }]);
        }

        else {
this.mylist.splice(this.mylist.indexOf(key), 1);

        }
      }

The function above is an event when a user clicks on a checkbox. When true the array must be filled with values. Else the array with the unique key must be removed to prevent duplicates

1 Answer 1

1

After the push statement, this.mylist will contain the array of arrays since you have pushed [{key:{"test1":name,"test":desc,"hi i am a test":other,"none":type,"amount":0} }] which is an array and you cannot access the array with the key (in this case you cannot access this.mylist[0], if the above array is added as first element, with this.mylist.indexOf(key))

One possible solution is you can make the type of mylist as object instead of array, then you can add elements to the Object like this

[{key:{"test1":name,"test":desc,"hi i am a test":other,"none":type,"amount":0} }];

this.mylist.key = {"test1":name,"test":desc,"hi i am a 
test":other,"none":type,"amount":0} }

later you can use checkbox function like this

CheckBox(values) {
    this.bool = (this.mylist.key === -1);
    if (this.bool) { 
        this.mylist.key={"key":key,"naam":naam,"beschrijving":beschrijving,"type":type,"aantal":aantal};
    } else {
        delete this.mylist.key; 
    }
}

The logic behind Checkbox function seems incorrect, as the function will be checking if mylist contains a key, if it's not present then add the key to mylist and removes if its present. This logic does not properly handle the removing of duplicates if that's your final goal.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.