Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda