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

79
Views
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 answers
Answer question

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 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!