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

142
Views
How to ensure only one value gets pushed Javascript array?

Using Vue.js 2. I have a list of objects I'm displaying to the user. An entity of that object specifically. Here's a screen of the object (only showing 1 for brevity) and the list it's displayed in

enter image description here

<v-sheet elevation="10" rounded class="py-4 px-1">
      <div class="text-center text-caption">
        Device Serial Number {{ this.deviceOptions.length }}
           </div>
             <v-chip-group
                multiple
                active-class="primary--text"
                v-model="deviceOptions"
              >
             <v-chip
                v-for="item in $store.state.responeArray"
                  :key="item.key"
                  @click="countDevices(item)" // pass this selected object to a method
             >
                  {{ item.DeviceSerialNumber }}
                </v-chip>
            </v-chip-group>
 </v-sheet>

enter image description here

When one of these items are clicked I pass the item object to a method where I add the CollectionId to an array if it doesn't exist.

The object gets passed correctly, and I check if it exists in the array. If it doesn't, I add it. If I deselect, I check and remove it. here's that method.

countDevices(e) {
      if (this.deviceOptions.indexOf(e.CollectionId) >= 0) {
        for(let i = 0; i < this.deviceOptions.length; i++) {
          if (this.deviceOptions[i] === e.CollectionId){
            this.deviceOptions.splice(i, 1);
            
          }
        }

        console.log(this.deviceOptions.length);
        console.log(this.deviceOptions);
      } else {
        this.deviceOptions.unshift(e.CollectionId);
        console.log(this.deviceOptions.length);
        console.log(this.deviceOptions);
      }
    },

The actual problem

However, when you add to the array, it adds two values. not just one. I can't tell what that extra value is, but I think it's the observer vue uses.

Take notice of the length displayed that console where the value is present and observer

enter image description here

There's only one item selected, but length is showing 2. Whereas my goal is to show the counted number of indexes within the array.

Can anyone assist in this?

enter image description here

about 4 years ago · Juan Pablo Isaza
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!