Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  87] [ 2]  / answers: 1 / hits: 38206  / 8 Years ago, wed, august 31, 2016, 12:00:00

How can I get top 5 max elements from array of ints with the standard library of es2015? Thanks for help.


More From » arrays

 Answers
16

A solution in ES6 :


values = [1,65,8,98,689,12,33,2,3,789];
var topValues = values.sort((a,b) => b-a).slice(0,5);
console.log(topValues); // [789,689,98,65,33]

Many others exist, ask if you need more


[#60853] Monday, August 29, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jack

Total Points: 557
Total Questions: 96
Total Answers: 80

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
jack questions
Sun, Jan 31, 21, 00:00, 3 Years ago
Thu, Dec 10, 20, 00:00, 4 Years ago
Sat, Aug 1, 20, 00:00, 4 Years ago
Fri, Apr 17, 20, 00:00, 4 Years ago
Wed, Aug 14, 19, 00:00, 5 Years ago
;