You can use es6, but you can

Posted by warriors2003 on Fri, 28 Jan 2022 15:40:17 +0100

1. Make complaints about value.

Values are very common in programs, such as values from object obj

const obj={
  a:1,
  b:2,  
c:3
}
Make complaints:
const a= obj.a
const b=obj.b
const c=obj.c
 perhaps
const f=obj.a+obj.b;
const g=obj.c+obj.e;
improvement:
const {a,b,c,d,e}=obj;
const f=a+b;
const g=c+e;

If the variable name created to the object is inconsistent with the object attribute, you can write it this way
const {a:a1}=obj;
console.log(a1)

supplement

Although the deconstruction assignment of es6 is easy to use. However, it should be noted that the deconstructed object cannot be undefined or null. Otherwise, an error will be reported, so you need to give the deconstructed object a default value.

const {a,b,c,d}=obj||{}

Two. Make complaints about merging arrays.

For example, merge two arrays and two objects

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

const obj1={a:1};const obj2={b:2};

const obj3=object.assign({},obj1,obj2);

Tucao: did the es6 expansion operator forget and make complaints about the combination of arrays?

improvement:

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

const b=[1,5,6];

const c=[..new Set([...a,...b])];

Three. Make complaints about the conditions of judgment in if.

if(type==a||type==1||type==6){...}

Tucao: will es6 array method make complaints about includes?

Improvement: const condition=[1,2,3,4,5,6];

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

Four. Make complaints about list search.

const a = [1,2,3,4,5];
const result = a.find(
item =>{
return item === 3
}
);console.log(result)
//3. Return the matching element find()

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

}

);console.log(result)
//[3] returns an array whose values are the corresponding elements that meet the conditions

5, Requirements for flat arrays

In the json data of a department, the attribute name is the id of the Department and the attribute value is the array set of department member IDs. Now we want to extract the member IDs of the Department into an array set.

const deps = {
'Purchasing Department':[1,2,3],
'Ministry of Personnel':[5,8,12],
'Administration Department':[5,14,79],
'Ministry of transport':[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)];console.log(member)
//(10) [1, 2, 3, 5, 8, 12, 14, 79, 64, 105]

Do you want to make complaints about traversing? Object. Have you forgotten the values? It also involves the flattening of the array. Why not provide the flat method? Fortunately, the depth of the array is only 2 dimensions. If you encounter the depth of 4 and 5 dimensions, do you want to loop nesting to flatten it?

const deps = {
    'Purchasing Department':[1,2,3],
    'Ministry of Personnel':[5,8,12],
    'Administration Department':[5,14,79],
    'Ministry of transport':[3,64,105],
}
let member=Object.values(deps).flat(Infinity);
//The new function in es2019, flat(), will recurse to the specified depth, connect all subarrays, and return a new array. The flat function receives a parameter whose value is the dimension of the array to be expanded, and infinity expands all nested arrays
//The arr.flat() method can remove empty items from the array.

Six. Make complaints about getting object attribute values.

const name=obj&&obj.name;

Improvement: will the optional chain operator in es6 be used?

const name=obj?.name;

Seven. Make complaints about adding object properties.

When adding attributes to an object, what should I do if the attributes change dynamically.

let obj={};let index=1;let key = `topic${index}`;

obj[key] = 'topic content';

let obj={};
let index1;
obj[`topic${index}`]='Topic content'

8, Judgment on non empty input box

When processing the business related to the input box, it is often judged that the input box is the input value

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

Tucao: es6 hollow hollows merge operator make complaints about it?

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

Nine. Make complaints about asynchronous functions.

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)
      })
   })
}

Tucao: if you call asynchronous function, you won't make complaints about the region.

const fn = async () =>{
  const res1 = await fn1();
  const res2 = await fn2();
  console.log(res1);// 1
  console.log(res2);// 2
}
//However, to make concurrent requests, you still need to use paromise.all().
const fn=()=>{
 promise.all([fn1(),fn2()]).then(res=>{
     console.log(res);
})
}
//If a concurrent request is made, as long as one of the asynchronous functions completes processing, the result will be returned, which needs to be used Promise.race().