es6的一些基本使用

一、关于取值

const obj = {
    
    
    a:1,
    b:2,
    c:3,
    d:4,
    e:5,
}
const a = obj.a;
const b = obj.b;
const c = obj.c;
const d = obj.d;
const e = obj.e;
const f = obj.a + obj.d;
const g = obj.c + obj.e;

es6

const {
    
    a,b,c,d,e} = obj;
const f = a + d;
const g = c+e;
//如果后端返回的数据不是你想要的字段
const {
    
    a:a1} = obj;
console.log(a);//1
//ES6的解构赋值虽然好用。但是要注意解构的对象不能为undefined、null。否则会报错,故要给被解构的对象一个默认值。
const {
    
    a,b,c,d,e} = obj || {
    
    };

二数据合并

const a = [1,2,3];
const b = [1,5,6];
const c = a.concat(b);//[1,2,3,1,5,6]

const obj1 = {
    
    
  a:1,
}
const obj2 = {
    
    
  b:1,
}
const obj = Object.assign({
    
    }, obj1, obj2);//{a:1,b:1}

es6

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}

三、关于拼接字符串

const name = '小明';
const score = 59;
let result = '';
if(score > 60){
    
    
  result = `${
      
      name}的考试成绩及格`; 
}else{
    
    
  result = `${
      
      name}的考试成绩不及格`; 
}

es6

const name = '小明';
const score = 59;
//在${}中可以放入任意的JavaScript表达式,可以进行运算,以及引用对象属性。
const result = `${
      
      name}${
      
      score > 60?'的考试成绩及格':'的考试成绩不及格'}`;

四、关于if中判断条件的吐槽

if(
    type == 1 ||
    type == 2 ||
    type == 3 ||
    type == 4 ||
){
    
    
   //...
}

es6

const condition = [1,2,3,4];

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

五、关于列表搜索

const a = [1,2,3,4,5];
const result = a.filter( 
  item =>{
    
    
    return item === 3
  }
)

es6

//find方法中找到符合条件的项,就不会继续遍历数组。
const a = [1,2,3,4,5];
const result = a.find( 
  item =>{
    
    
    return item === 3
  }
)

六、关于扁平化数组

const deps = {
    
    
'采购部':[1,2,3],
'人事部':[5,8,12],
'行政部':[5,14,79],
'运输部':[3,64,105],
}
let member = [];
for (let item in deps){
    
    
    const value = deps[item];
    if(Array.isArray(value)){
    
    
        member = [...member,...value]
    }
}
member = [...new Set(member)]

es6

const deps = {
    
    
    '采购部':[1,2,3],
    '人事部':[5,8,12],
    '行政部':[5,14,79],
    '运输部':[3,64,105],
}
let member = Object.values(deps).flat(Infinity);

七、关于获取对象属性值

const name = obj && obj.name;

es6

const name = obj?.name;

八、关于添加对象属性

let obj = {
    
    };
let index = 1;
let key = `topic${
      
      index}`;
obj[key] = '话题内容';

es6

let obj = {
    
    };
let index = 1;
obj[`topic${
      
      index}`] = '话题内容';

九、关于输入框非空的判断

if(value !== null && value !== undefined && value !== ''){
    
    
    //...
}

es6

if((value??'') !== ''){
    
    
  //...
}

十、关于异步函数

const fn1 = () =>{
    
    
  return new Promise((resolve, reject) => {
    
    
    setTimeout(() => {
    
    
      resolve(1);
    }, 300);
  });
}
const fn2 = () =>{
    
    
  return new Promise((resolve, reject) => {
    
    
    setTimeout(() => {
    
    
      resolve(2);
    }, 600);
  });
}
const fn = () =>{
    
    
   fn1().then(res1 =>{
    
    
      console.log(res1);// 1
      fn2().then(res2 =>{
    
    
        console.log(res2)
      })
   })
}

es6

const fn = async () =>{
    
    
  const res1 = await fn1();
  const res2 = await fn2();
  console.log(res1);// 1
  console.log(res2);// 2
}
const fn = () =>{
    
    
   Promise.all([fn1(),fn2()]).then(res =>{
    
    
       console.log(res);// [1,2]
   }) 
}

猜你喜欢

转载自blog.csdn.net/weixin_41688609/article/details/129086203
今日推荐