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

265
Views
javascript map, grab first index with a certain element

As you can see from my snack expo example here, I am grabbing the first index and last index of my array successfully. I am doing this so I can add a borderRadius to the top and bottom of my view. I would eventually like to have a section for online users as well as a section for offline users.

I want to be able to grab the index of the first online user in the array regardless of its position in the array overall. So if the first three indexes have the status of offline, and the fourth index is online, I want to be able to grab that fourth index because it is the first time an online status appears.

For further clarification, if you look at my example, if the last index of the entire array had the status of online, the borderRadius would not be set. If the first index of the array did not have the status of online, the borderRadius would not be set.

grab first index in entire array regardless of status -> I need this to be the first online index, not the first index in entire array

else if(i == 0){
        return ()

grab last index in entire array -> I need this to be the last index with the online status, not the last index in the entire array

if (i + 1 === List.length) {
        return ()
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can compute the first and last indexes before iterating on your users list( see https://snack.expo.dev/83rFQLCrF )

const lastIndex = List.map(e => e.status).lastIndexOf('online');
const firstIndex = List.map(e => e.status).indexOf('online');

const renderList = (props) => {
    return List.map((item, i) => {
      /* grabs last index, I need to grab the last index with status online */
      if (i  === lastIndex) {
       // TO DO
      } 
      /* grabs first index, I need to grab the first index with status online */
      else if(i == firstIndex ){
       // TO DO
      }
      else{
         // TO DO
      }

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