MENU

javascript秘籍

December 5, 2018 • 技术

  • 快速去重:
Array.from(new Set(arr));


  • 数组求和:
1.eval(arr.join('+'))

2.arr.reduce((pre, cur) => pre + cur)

  • 稳定排序:
arr.sort((a, b) => a - b)//从小到大
arr.sort((a, b) => b - a)//从大到小



  • 修复精度错误:
var x = 0.9 - 0.8
console.log(x)
console.log(x.toFixed(2))

  • 进制转换:
//1.十进制转其他
var x = 10
console.log(x.toString(8))
console.log(x.toString(32))
console.log(x.toString(16))

//1.其他转十进制
var x = 100
console.log(parseInt(x, 2))
console.log(parseInt(x, 8))
console.log(parseInt(x, 16))

  • 数组展开
var arr = [1, 2, 3, 4, 5]
console.log(...arr)

  • 数组中最大数
var arr = [1, 2, 3, 4, 5]
console.log(Math.max(arr))

  • 同理数组中最小数
var arr = [1, 2, 3, 4, 5]
console.log(Math.min(...arr))

Last Modified: July 8, 2019