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

95
Views
JS slice last element DESC

I have array [1, 2, 3, 4, 5, 6]
How to get last 3 elements 6, 5, 4 by DESC

I use slice but I get the items in the wrong order

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

it seems you want to reverse the array you have sliced.

let arr = [1, 2, 3, 4, 5, 6]
let newArr = arr.slice(-3).reverse(); 
console.log(newArr)

if your last 3 elements are not in ascending order already in the original array you can use sort after slice. Well you can use sort for your original problem as well

let arr = [1, 2, 3, 5, 4, 6]

let newArr2 = arr.slice(-3).sort((a,b)=>b-a);
console.log(newArr2)

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!