Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda