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

106
Views
React array.slice multiple endpoints

Is it possible to get first 3 and last 2 items using array.slice in js

I want to render all data if user is AUTH, and if not I want first 3 and last 2 from array without recreating array.

 {menu.slice(...(auth ? [0, 5] : //THERE SHOULD FIND FIRST 3 AND LAST 2 )).map((item) => (
      <Link
        onClick={() => dispatch(handleMenu())}
        to={item.path}
        className={`${auth === false && "font-md"} ${
          item.path === window.location.pathname && "font-2xl"
        }`}
      >
        {item.title}
      </Link>
    ))}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Yes, it is possible to get first 3 items and last 2 items;

example:

let arr = ["a", "b", "d", "c", "e", "f", "g", "h", "i"];

To get first 3 items:

arr.slice(0, 3);

To get last 2 items:

arr.slice(-2);

As you wanted to add both of the slices and render them, this might help you:

arr.slice(0, 3).concat(arr.slice(-2));
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!