Array.from()
The array.from () method converts an array-like object or traversable object into a real Array.
What is an array-like object: an object with the length attribute.
ifArray.from
The argument is a class array object
To convert an array-like object into a true array, the following conditions must be met:
- The class array object must have
length
Properties. If not, an empty array is returned. - Object of the class array
key
The value must be a numeric or string number. Array elements areundefind
Example:
let array = {
0: 'tom'.1: '65'.2: 'male'.3: ['jane'.'john'.'Mary'],}console.log(Array.from(array)); / / []
array.length = 4;
console.log(Array.from(array)); / / [' Tom ', '65' and 'male', [' Jane ', 'John', 'Mary']]
array.length = 5;
array.name = 'name';
console.log(Array.from(array)); / / [' Tom ', '65' and 'male', [' Jane ', 'John', 'Mary'], undefind]
Copy the code
ifArray.from
Parameter is a string
let str = 'hello world! ';
console.log(Array.from(str)) // ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", "!"]
Copy the code
ifArray.from
There are two parameters
The second argument, of type FUN, iterates through and processes each element, putting the processed value into the returned array.
let arr = [12.45.97.9797.564.134.45642]
let set = new Set(arr)
console.log(Array.from(set, item= > item + 1)) // [13, 46, 98, 9798, 565, 135, 45643]
Copy the code