- Learning JavaScript Data Structures and Algorithms
- Loiane Groner
- 98字
- 2021-08-27 18:41:18
Using the from method
The Array.from method creates a new array from an existing one. For example, if we want to copy the array numbers into a new one, we can use the following code:
let numbers2 = Array.from(numbers);
It is also possible to pass a function so that we can determine which values we want to map. Consider the following code:
let evens = Array.from(numbers, x => (x % 2 == 0));
The preceding code created a new array named evens, and a value true if in the original array the number is even, and false otherwise.