It converts any data structures that implement Iterator
interface to array.
Iterable data structures include:
- Array
- String
- Map
- Set
- arguments
- DOM data structures
Please note
WeakMap
,WeakSet
andplain object
are not iterable.
Array.from('hello'); // ['h', 'e', 'l', 'l', 'o']
Array.from([1, 2, 3]); // [1, 2, 3]
Array.from([1, 2, 3], x => x + x); // [2, 4, 6]
Array.from({length: 5}, (v, i) => i); // [0, 1, 2, 3, 4]
const s = new Set();
[2, 3, 5, 4, 5, 2, 2].forEach(x => s.add(x));
Array.isArray(s); // false
Array.isArray(Array.from(s)); // true
Array.from(arguments) // gives you the array
It's used to create an array in a consistent way (no function overriding).
// old way
Array() // []
Array(3) // [, , ,] empty slots not undefined
Array.from({length: 3}) // [undefined, undefined, undefined]
Array(3, 11, 8) // [3, 11, 8]
// new way
Array.of(3, 11, 8) // [3, 11, 8]
Array.of(3) // [3]
Array.of() // []
['a', 'b', 'c'].fill(7) // [7, 7, 7]
['a', 'b', 'c'].fill(7, 1, 2) // ['a', 7, 'c']
// 2nd arg - start index
// 3nd arg - end index exclusive
~-1
is 0. ~x
is equal to -(x+1)
.
if (~[1, 2].indexOf(1)) {
// if array has element 1 dosomething
}
// from end
[1,2,3,4,5].slice(-3); // [2,3,4]
[1,2,3,4,5].slice(-1); // [5]
// from start
var array = [11, 12, 13, 14, 15];
array.length = 3;
console.log(array); // [11,12,13]
array.length = 0;
console.log(array); // []