ES6追加方法

1.合并数据
const a = [1,2,3];
const b = [1,5,6];
const c = [...new Set([...a,...b])];//[1,2,3,5,6]

const obj1 = {
  a:1,
}
const obj2 = {
  b:1,
}
const obj = {...obj1,...obj2};//{a:1,b:1}
2.if 条件判断
const condition = [1,2,3,4];

if( condition.includes(type) ){
   //...
}

等同于 if(type==1||type==2){}
条件少 this.currentIndex = this.current === 1 || this.current === 3

条件多 this.currentIndex = [1, 3].indexOf(this.current) > -1

3.数组扁平化
const deps = {
    '采购部':[1,2,3],
    '人事部':[5,8,12],
    '行政部':[5,14,79],
    '运输部':[3,64,105],
}
let member = Object.values(deps).flat(Infinity);
4.输入框非空的判断
if(value !== null && value !== undefined && value !== ''){
    //...
}
等同于
if((value??'') !== ''){
  //...
}
5.判断数组是否为空
const isNotEmpty = arr => Array.isArray(arr) && arr.length > 0

isNotEmpty([1, 2, 3]) //true
6.获取两个整数之间的随机整数
const random  = (min,max) => Math.floor(Math.random * (max-min+1) +min)
random(1,50)
7.求一组数的平均值
const average = (...args) => args.reduce((a,b) => a+b)/args.length

average(1,2,3,4,5)
posted @ 2022-02-25 10:18  Smile浅笑  阅读(139)  评论(0编辑  收藏  举报