js如何获取数组中指定个数的几个最大值

数组如下:

var arr = [1,5,3,2,10,15]

如何得到3个或N个最大值得数组

arr2 = [5,10,15]


明月笑刀无情
浏览 1770回答 1
1回答

慕运维8079593

var arr = [1,5,3,2,10,15];&nbsp; function maxarr(arr,len){&nbsp; &nbsp; &nbsp; var max = [];&nbsp; &nbsp; arr.sort(function(a,b){&nbsp; &nbsp; &nbsp; &nbsp;return a-b;&nbsp; &nbsp;});&nbsp; &nbsp;for(var i=0;i<len;i++){&nbsp; &nbsp; &nbsp; &nbsp;max.push(arr.pop());&nbsp; &nbsp;}&nbsp; &nbsp;return max.reverse();&nbsp; }&nbsp; &nbsp;console.log(maxarr(arr,4));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript