This is the 13th day of my participation in Gwen Challenge

1, use Set to implement array deduplication

In ES6, because a Set stores only unique values, you can use a Set to remove duplicates.

let arr = [1.1.2.2.3.3];
let deduped = [...new Set(arr)] / / [1, 2, 3]
// reduce array objects are deduplicated
var obj = {};
points = points.reduce(function(list, next) {
    obj[next.lat] ? ' ' : obj[next.lat] = true && list.push(next);
    returnlist; } []);console.log(points)  ;  
Copy the code

2. Merge objects

ES6 brings extension operators (…) . It’s usually used to deconstruct arrays, but you can also use it for objects. It can also be used for shallow copies, and for deep copies when there is only one layer of data (as shown below). Deep copy implementation: juejin.cn/post/697084…

Next, we expand a new object using the extension operator, and the property values in the second object overwrite the property values of the first object. For example, the b and c of object2 overwrite the same attribute of Object1.

let object1 = { a:1.b:2.c:3 }
let object2 = { b:30.c:40.d:50}
letMerged = {... Object1,... object2}console.log(merged) // {a:1, b:30, c:40, d:50}
Copy the code

3. Structure assignment

let [a, b, c] = [1.2.3];
// Define three variables and assign corresponding values; If the number of values does not match the number of variable names, no corresponding variable value is undefined
Copy the code

4. Modularization

/ / a. s definition
let a = {a:1.b:2.c:3};
export default a;

/ / introduction
import a from 'a.js';
console.log(a.a); 

// c.js 
export const arr = [{a: 1}, {b:2}]
/ / introduction
import {arr} from 'c.js'

Copy the code

Export and export default:

  • Export and export default can be used to export constants, functions, files, and modules
  • Export default is the default external interface of the module. There is only one external interface, so it can only appear once.
  • Export can be exported directly or defined first and then exported. There can be more than one.

5. Array filtering

var newarr = [
  { num: 1.val: 'ceshi'.flag: 'aa' },
  { num: 2.val: 'ceshi2'.flag: 'aa2'}]console.log(newarr.filter(item= > item.num===2 ))
// [{"num":2,"val":"ceshi2","flag":"aa2"}]
Copy the code

6. Default parameters

function show_hi(s='hi') {

    console.log(s)
    // hi
}

Copy the code

String template ‘ ‘

var name = `my name is ${username} `;
Copy the code