JS - Get top 5 max elemenets from array
up vote
0
down vote
favorite
How can I get top 5 max elements from array of ints, which in its turn is a property of js object.
Thanks for help.
javascript arrays
add a comment |
up vote
0
down vote
favorite
How can I get top 5 max elements from array of ints, which in its turn is a property of js object.
Thanks for help.
javascript arrays
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How can I get top 5 max elements from array of ints, which in its turn is a property of js object.
Thanks for help.
javascript arrays
How can I get top 5 max elements from array of ints, which in its turn is a property of js object.
Thanks for help.
javascript arrays
javascript arrays
asked Aug 31 '16 at 16:01
Dmitriy Kovalenko
1,110419
1,110419
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
A solution in ES6 :
values = [1,65,8,98,689,12,33,2,3,789];
var topValues = values.sort((a,b) => a>b).slice(0,5);
console.log(topValues); // [ 1, 2, 3, 8, 12 ]
Many others exist, ask if you need more
2
(a,b) => a>b
- well, not really.
– georg
Aug 31 '16 at 17:18
[2, 6, 8, 1].sort((a, b) => b>a) [2, 6, 8 1] incorrect [2, 6, 8, 1].sort((a, b) => b - a) [8, 6, 2, 1] correct, the sort function should be(a,b) => b - a
– Cody Moniz
Nov 9 at 21:39
add a comment |
up vote
0
down vote
[2, 6, 8, 1, 10, 11].sort((a, b) => b - a).slice(0,5)
[11, 10, 8, 6, 2]
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
A solution in ES6 :
values = [1,65,8,98,689,12,33,2,3,789];
var topValues = values.sort((a,b) => a>b).slice(0,5);
console.log(topValues); // [ 1, 2, 3, 8, 12 ]
Many others exist, ask if you need more
2
(a,b) => a>b
- well, not really.
– georg
Aug 31 '16 at 17:18
[2, 6, 8, 1].sort((a, b) => b>a) [2, 6, 8 1] incorrect [2, 6, 8, 1].sort((a, b) => b - a) [8, 6, 2, 1] correct, the sort function should be(a,b) => b - a
– Cody Moniz
Nov 9 at 21:39
add a comment |
up vote
2
down vote
accepted
A solution in ES6 :
values = [1,65,8,98,689,12,33,2,3,789];
var topValues = values.sort((a,b) => a>b).slice(0,5);
console.log(topValues); // [ 1, 2, 3, 8, 12 ]
Many others exist, ask if you need more
2
(a,b) => a>b
- well, not really.
– georg
Aug 31 '16 at 17:18
[2, 6, 8, 1].sort((a, b) => b>a) [2, 6, 8 1] incorrect [2, 6, 8, 1].sort((a, b) => b - a) [8, 6, 2, 1] correct, the sort function should be(a,b) => b - a
– Cody Moniz
Nov 9 at 21:39
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
A solution in ES6 :
values = [1,65,8,98,689,12,33,2,3,789];
var topValues = values.sort((a,b) => a>b).slice(0,5);
console.log(topValues); // [ 1, 2, 3, 8, 12 ]
Many others exist, ask if you need more
A solution in ES6 :
values = [1,65,8,98,689,12,33,2,3,789];
var topValues = values.sort((a,b) => a>b).slice(0,5);
console.log(topValues); // [ 1, 2, 3, 8, 12 ]
Many others exist, ask if you need more
answered Aug 31 '16 at 16:07
kevin ternet
2,458716
2,458716
2
(a,b) => a>b
- well, not really.
– georg
Aug 31 '16 at 17:18
[2, 6, 8, 1].sort((a, b) => b>a) [2, 6, 8 1] incorrect [2, 6, 8, 1].sort((a, b) => b - a) [8, 6, 2, 1] correct, the sort function should be(a,b) => b - a
– Cody Moniz
Nov 9 at 21:39
add a comment |
2
(a,b) => a>b
- well, not really.
– georg
Aug 31 '16 at 17:18
[2, 6, 8, 1].sort((a, b) => b>a) [2, 6, 8 1] incorrect [2, 6, 8, 1].sort((a, b) => b - a) [8, 6, 2, 1] correct, the sort function should be(a,b) => b - a
– Cody Moniz
Nov 9 at 21:39
2
2
(a,b) => a>b
- well, not really.– georg
Aug 31 '16 at 17:18
(a,b) => a>b
- well, not really.– georg
Aug 31 '16 at 17:18
[2, 6, 8, 1].sort((a, b) => b>a) [2, 6, 8 1] incorrect [2, 6, 8, 1].sort((a, b) => b - a) [8, 6, 2, 1] correct, the sort function should be
(a,b) => b - a
– Cody Moniz
Nov 9 at 21:39
[2, 6, 8, 1].sort((a, b) => b>a) [2, 6, 8 1] incorrect [2, 6, 8, 1].sort((a, b) => b - a) [8, 6, 2, 1] correct, the sort function should be
(a,b) => b - a
– Cody Moniz
Nov 9 at 21:39
add a comment |
up vote
0
down vote
[2, 6, 8, 1, 10, 11].sort((a, b) => b - a).slice(0,5)
[11, 10, 8, 6, 2]
add a comment |
up vote
0
down vote
[2, 6, 8, 1, 10, 11].sort((a, b) => b - a).slice(0,5)
[11, 10, 8, 6, 2]
add a comment |
up vote
0
down vote
up vote
0
down vote
[2, 6, 8, 1, 10, 11].sort((a, b) => b - a).slice(0,5)
[11, 10, 8, 6, 2]
[2, 6, 8, 1, 10, 11].sort((a, b) => b - a).slice(0,5)
[11, 10, 8, 6, 2]
answered Nov 9 at 21:40
Cody Moniz
1,91521215
1,91521215
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39254218%2fjs-get-top-5-max-elemenets-from-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown