微信小程序开发交流qq群 173683895
承接微信小程序开发。扫码加微信。
根据数组中某个参数的值的大小进行升序
<script type="text/javascript">function compare(val) {return function (a, b) {var value1 = a[val];var value2 = b[val];return value1 - value2;}}var arr =[{a:3},{a:2},{a:1},{a:4},{a:6},{a:0}];arr.sort(compare('a'));console.log(arr);</script>
打印结果"[{"a":0},{"a":1},{"a":2},{"a":3},{"a":4},{"a":6}]"
取数组中最小值 打印结果 1
<script type="text/javascript">var arr =[1,2,3,4,5,6];var b= Math.min.apply(null, arr)console.log(b)</script>
取数组中最大值 打印结果 6
<script type="text/javascript">var arr =[1,2,3,4,5,6];var b= Math.max.apply(null, arr)console.log(b)</script>
根据数组中某个参数的值的大小进行倒序
<script type="text/javascript">var arr =[{a:3},{a:2},{a:1},{a:4},{a:6},{a:0}];arr.sort(compare('a')).reverse();console.log(JSON.stringify(arr));function compare(val) {return function (a, b) {var value1 = a[val];var value2 = b[val];return value1 - value2;}}</script>
打印结果"[{"a":6},{"a":4},{"a":3},{"a":2},{"a":1},{"a":0}]"
微信小程序开发交流qq群 581478349
承接微信小程序开发。扫码加微信。