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

80
Vistas
Splice removing smallest ID not specific item

Live counter of who is viewing a news article on a website. Laravel Jetstream.

 .leaving(user => {
     this.participants.splice( this.participants.indexOf(user, 1));
})

 .joining(user => {
             this.participants.push(user);
            this.count++;
   })

However I noticed that if there are two users on the page it keeps removing the one with the smallest ID value. Tested the theory successfully.

So if joe logs in user ID 100 and then Jimmy logs in ID 200. Shows both users in a list.

Jimmy's screen:

  • Joe
  • Jimmy

If Joe refreshes the page it removes Jimmy from the active users and places him in again as an active, Shows him twice on his screen after the refresh. The items remain correct for Joe regardless of who refreshes (enters or leaves).

Jimmy's screen:

  • Joe
  • Joe

It's like the index isn't correct and hence not splicing properly just removing the last item on the list and repopulating it with the lower ID user.

almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

However I noticed that if there are two users on the page it keeps removing the one with the smallest ID value.

Makes sense because probably you have the array ordered and indexOf return the first ocurrence found.

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

So probably you have to filter differently before doing the splice...

almost 4 years ago · Santiago Trujillo Denunciar
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