1. 合并对象和数组(使用扩展运算符,合并数组考虑去重) ```vue const a = [1,2,3];

    const b = [1,5,6];

    const c = […new Set([…a,…b])]; //[1,2,3,5,6]

    1. ```vue
    2. const obj1 = {
    3. a:1,
    4. }
    5. const obj2 = {
    6. b:1,
    7. }
    8. const obj = {...obj1,...obj2}; //{a:1,b:1}
    1. 拼接字符串

    ${}中可以放入任意的JavaScript表达式,可以进行运算,以及引用对象属性。

    1. const name = '小明';
    2. const score = 59;
    3. let result = '';
    4. if(score > 60){
    5. result = `${name}的考试成绩及格`;
    6. }else{
    7. result = `${name}的考试成绩不及格`;
    8. }
    1. const name = '小明';
    2. const score = 59;
    3. const result = `${name}${score > 60?'的考试成绩及格':'的考试成绩不及格'}`;
    1. 判断条件优化(使用数组实例方法includes)
      1. if(
      2. type == 1 ||
      3. type == 2 ||
      4. type == 3 ||
      5. type == 4 ||
      6. ){
      7. //...
      8. }
      ```vue const condition = [1,2,3,4];

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

    1. 4.列表搜索的精准搜索和模糊搜索(精准搜索用find找到后不会再遍历,节约性能,模糊搜索用filter)
    2. ```vue
    3. const a = [1,2,3,4,5];
    4. const result = a.find(
    5. item =>{
    6. return item === 3
    7. }
    8. )
    1. 数组扁平化

    一个部门JSON数据中,属性名是部门id,属性值是个部门成员id数组集合,现在要把有部门的成员id都提取到一个数组集合中;获取对象的全部值要用Object.values,Infinity作为flat的参数可以使多维度数组扁平化; flat方法不支持IE浏览器;

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

    6.添加对象属性(给对象添加属性时,如果属性名是动态变化)

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

    7.输入框非空的判断(ES6中新出的空值合并运算符 ??’’)

    1. if(value !== null && value !== undefined && value !== ''){
    2. //...
    3. }
    1. if((value??'') !== ''){
    2. //...
    3. }