Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Adding and removing an element in an array

There are 2 toggle buttons. If the value is true, then add to the array, otherwise remove the element.

data:

originality: []

toggles:

<toggle id='1' ref='toggleOriginal'> Click </toggle>
<toggle id='2' ref='toggleAnalog'> Click </toggle>

methods:

 if(this.$refs.toggleOriginal.isActive) {
    this.originality.push(this.$refs.toggleOriginal.id);
 } else {
    this.originality = this.originality.filter((item) => {
      return item == this.$refs.toggleOriginal.id;
   });
 }

 if(this.$refs.toggleAnalog.isActive) {
    this.originality.push(this.$refs.toggleAnalog.id);
  } else {
    this.originality = this.originality.filter((item) => {
      return item == this.$refs.toggleAnalog.id;
    });
  }

And the same for the second. In isActive I check for true / false. the problem is that if two toggle is true and I want to convert one to false, then the wrong element is removed. Perhaps you should use a different functionality?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

To remove from array using filter, you should test for non-equality. Your example uses equality, and that will remove all but your item.

this.originality = this.originality.filter((item) => {
      return item !== this.$refs.toggleOriginal.id;
   });

or splice

const index = this.originality.indexOf(this.$refs.toggleOriginal.id)
this.originality.splice(index, 1)
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!