前端技巧(一)

35 阅读1分钟

一、用对象更方便

  const list = new Map().set('a', ['a1', 'a2']).set('b', ['b1', 'b2']).set('c', ['c1', 'c2']);
  const selectList = (select) => list.get(select);
  console.log('selectList', selectList('a')); // ['a1', 'a2']

二、解构删除一个对象属性

  const person = {
    name: 'job',
    age: '12',
    height: '178',
    work: 'palying'
  }

  const { work, ...newPerson } = person;
  console.log(newPerson); 
  // {
  //   name: 'job',
  //   age: '12',
  //   height: '178',
  // }

三、使用Set去重数组

const arr = [1, 2, 2, 3, 3, 3, 4, 6];
const newArr = [...new Set(arr)] // [1, 2, 3, 4, 6]