一、过滤数据中的false等
*
* @description 过滤数据中的false
* @param { arr} 过滤数组
* /
const filterFalse = ( arr) = > {
if ( ! arr || arr. length == 0 ) return
return arr. filter ( Boolean)
} ;
const ele = [ 23 , 11 , "123" , '&&' , null, false ]
console. log ( filterFalse ( ele) )
二、空值合并运算符
const nullKey = null
const empty = ''
const somenum = 12
const d = nullKey? ? 'dd'
const e= empty? ? 'ee'
const f = somenum? ? 'ff'
console. log ( d, e, f)
三、从数组中获取最大值和最小值
const numbers = [ 12 , 23 , 112 , 3 , 1 , 23 , 1 , 23 ]
const max = Math. max ( . . . numbers)
const min = Math. min ( . . . numbers)
console. log ( max, min)
四、RGB 与 HEX的转换
const rgbToHex = ( r, g, b) = > {
const toHex = ( num) = > {
const hex = num. toString ( 16 )
return hex. length == = 1 ? `0 ${ hex} ` : hex
}
return `#${ toHex ( r) } ${ toHex ( g) } ${ toHex ( b) } `
} ;
console. log ( rgbToHex ( 17 , 65 , 35 ) )
const hexToRgb = ( hex) = > {
let arr = [ hex. slice ( 1 , 3 ) , hex. slice ( 3 , 5 ) , hex. slice ( 5 , 7 ) ]
let res = arr. map ( item = > {
return parseInt ( item, 16 )
} )
return `rgb ( ${ res. join ( ',' ) } ) `
} ;
console. log ( hexToRgb ( "#114123" ) )
五、清空数组
const arrs = [ 1 , 23 , 3 , 12 , 3 , 1 , ]
arrs. length= 0
console. log ( arrs)
六、保留指定位小数
const nums = 0.123124124
const fixed1 = nums. toFixed ( 4 )
const fixed2 = nums. toFixed ( 6 )
console. log ( fixed1, fixed2)
七、数组去重
const ARR = [ 11 , 22 , 11 , 21 , 1 , 1 , 1 , 1 , 2 , 22 , 11 ]
const res = [ . . . new Set ( ARR) ]
console. log ( res)
八、冻结对象
const Obj = {
key: '1' ,
value: '123' ,
}
Object. freeze ( Obj)
Obj. value = "10"
console. log ( Obj)
九、从数组中随机选择一个值
const ele = [ 23 , 11 , "123" , '&&' , null, false ]
const random = ( arr) = > arr[ Math. floor ( Math. random ( ) * arr. length) ]
const randomEle = random ( ele)
console. log ( randomEle)
十、 交换变量的值
let aa = '张三'
let bb = '李四'
[ aa, bb] = [ bb, aa]
console. log ( aa, bb)
十一、解构赋值
const object = {
key: '1' ,
value: '2' ,
array: [ "a" , "b" , "c" ]
}
const { key, value, array} = object
const [ a, b, c] = array
console. log ( key, value, array, a, b, c)
十二、=== 和 == 的区别
0 == false
0 == = false
1 == "1"
1 == = "1"
null == undefined
null == = undefined
十三、合并多个对象
const obj1 = {
key: '1' ,
value: '2'
}
const obj2 = {
name: 5 ,
index: '3'
}
const obj = { . . . obj1, . . . obj2}
console. log ( obj)
十四、十进制与十六进制和二进制的转换
const number = 52
const binaryNum = number. toString ( 2 )
const hexNum = number. toString ( 16 )
console. log ( binaryNum, hexNum)
const hexToNum = parseInt ( hexNum, 16 )
const binaryToNum = parseInt ( binaryNum, 2 )
console. log ( hexToNum, binaryToNum)
十五、反转字符串
const reverse = ( str, separator) = > {
return str. split ( separator) . reverse ( ) . join ( separator)
}
const sentence = '12k Lucjking false happy!!'
const reverseOne = reverse ( sentence, '' )
console. log ( reverseOne)
const reverseTwo = reverse ( reverseOne, ' ' )
console. log ( reverseTwo)
十六、打乱数据顺序
let arr = [ '12' , 111 , 'long' , false ]
arr = arr. sort ( ( ) = > 0.5 - Math. random ( ) )
console. log ( arr)
let str = '12312 dfsfash^ ^ ^ ^ ^ 8 r2h3uriuidij2o1i3h423&& & '
let num = str. replace ( / \D/ g, '' )
console. log ( num)