Array.from () method of a class is to convert the array can traverse the object or objects into a real array. The so-called class array of objects, the most basic requirement is that the object has a length property.
An array-converted object to a true array, must meet the following conditions:
1, such an array of objects must have attributes length, specifies the length of the array. If there is no length property, the array is converted to an empty array.
2, the class property name is an array of objects must be numeric or string of numbers.
ps: the name of the class attribute array object can be quoted, may not be quoted.
Array.from can accept three types of parameters:
1,Array.from (obj, mapFn)
// array members means a boolean value of false 0 Array.from ([. 1,, 2,3,3], X => X || 0) // [1,0,2,3,3] // The array-like object into an array, and multiplying 2 times the original base let arrayLike = { '0': '2', '1': '4', '2': '5', length:. 3 } Array.from(arrayLike, x => x*2) //[4,8,10] // to set the object into an array a, and multiplying the original two-fold basis Array.from ( new new the Set ([1,2,3,4]), X => X * 2) // [2,4 , 6,8]
2,Array.from ({length:n}, Fn)
Array.from({length:3}, () => 'jack') //["jack", "jack", "jack"] Array.from({length:3}, item => (item = {'name':'shao','age':18})) //[{'name':'shao','age':18}, {'name':'shao','age':18}, {'name':'shao','age':18}] Array.from({length: 2}, (v, i) => item = {index:i});//生成一个index从0到4的数组对象[{index: 0},{index: 1}]
3,Array.from(string)
Array.from('abc') //['a','b','c']
new Set () method to re-array
ES6 added Set this data structure, similar to the array, but members of the Set of unique. Based on this characteristic, it is very suitable to be used to make an array of heavy.
function distinct(a, b) { return Array.from(new Set([...a, ...b])) }
A method for ... of + Object array deduplication
First, create an empty object, and then use the for loop iterates, using the properties of an object will not be repeated this feature, check the array elements are repeated.
function distinct(a, b) { let arr = a.concat(b) let result = [] let obj = {} for (let i of arr) { if (!obj[i]) { result.push(i) obj[i] = 1 } } return result }
For the order of execution promise, async and await the
async function async1(){ console.log('async1 start') await async2() console.log('async1 end') } async function async2(){ console.log('async2') } console.log('script start') setTimeout(function(){ console.log('setTimeout') },0) async1(); new Promise(function(resolve){ console.log('promise1') resolve(); }).then(function(){ console.log('promise2') }) console.log('script end')
The print order of the above code as follows:
script start
async1 start
async2
promise1
script end
promise2
async1 end
setTimeout
have to be aware of is:
- setTimeout the timing synchronization is not performed immediately 0, but executed asynchronously.
- Promise function synchronously.
- After the execution resolve the Promise, then () is not executed synchronously, but asynchronously.