set
Set as ES6 new data deconstruction (like arrays), its members are unique, because the most direct usage scenario is to go heavy, and poor, the intersection of use. It uses the algorithm called "Same-value-zero equality", similar to the accurate calculation === mainly NaN, where it will be treated as two equal
// add Set commonly used methods and properties instance, Delete, Clear, has, size const new new the Set S = ([ 'A', 'B', 'C']); the console.log (S); {// the Set 'A', 'B', 'C'} the console.log (s.has ( 'A')) // to true, BOOL value of the console.log (s.size). 3 // the console.log (s.clear ( )) {} // the Set the console.log (s.delete ( 'A')) // to true, BOOL value
Set while Examples with conventional traversal method, and implement, intersection, difference sets.
const a =[1, 2, 3] const b = [2, 3, 4]; // 并集 const s = Array.from(new Set([...a, ...b])); // [ 1, 2, 3, 4 ] // 交集、差集 const bSet = new Set(b); const interestSet = a.filter(v => bSet.has(v)); // [ 2, 3 ] const interestSet = a.filter(v => !bSet.has(v)); // [ 1 ]
// remove duplicate member of an array of [... new Set (array)]
The above method can also be used to remove repeated character string inside.
[...new Set('ababbc')].join('') // "abc"
function dedupe(array) { return Array.from(new Set(array)); } dedupe([1, 1, 2, 3]) // [1, 2, 3]
Exponentiation operator (**), which is conjugated to one example, follow from Ruby and grammar, use more concise Math.pow(3, 2) === 3 ** 2 // 9
async, await an asynchronous solution
async function asyncFunc(params) { const result1 = await this.login() const result2 = await this.getInfo() }
Object.entries()
It can be enumerated attribute value of an object which will be returned in a two-dimensional array. (If the target object is an array, the array index will be returned as the key)
Object.entries({ one: 1, two: 2 }) //[['one', 1], ['two', 2]] Object.extries([1, 3]) //[['0', 1], ['1', 3]]
Object.values()
It works and Object.entries () method like, but it only returns the value of the key, the result is a one-dimensional array
Object.values({one: 1, two: 2}) // [1, 2] Object.values({3: 'a', 1: 'b', 2: 'c'}) // ['b', 'c', 'a'] Object.extries([1, 3]) //[1, 3]
String filled padStart (), padEnd ()
ES8 filling provides a new string, the method may be such that the string reaches a fixed length. It has two parameters, the string length and the target filling contents
'react'.padStart(10, 'm') //'mmmmmreact' 'react'.padEnd(10, 'm') //' reactmmmmm' 'react'.padStart(3, 'm') // 'vue'