How to use Array.from Method In Javascript
We can transform the javascript iterator into a JavaScript array by using the array.from() method.
Example:
const array = ['Geeks', 'for', 'Geeks'];
const mp = array.map(function (val) {
return val;
})
const it = mp.entries();
console.log(it)
let newArr=[];
newArr = Array.from(it);
console.log(newArr);
Output
Object [Array Iterator] {} [ [ 0, 'Geeks' ], [ 1, 'for' ], [ 2, 'Geeks' ] ]
How to transform a JavaScript iterator into an array ?
The task is to convert an iterator into an array, this can be performed by iterating each value of the iterator and storing the value in another array.
These are the following ways we can add an iterator to an array:
Table of Content
- Using Symbol iterator Property
- Using Array.from Method
- Using the Spread Operator
- Using a for…of Loop