You can use the Map
method to iterate through an array and perform operations on each element. Here’s an example that doubles each value in an array:
let numbers = [1, 2, 3, 4, 5];
let doubledNumbers = Array.from(numbers, x => x * 2);
console.log(doubledNumbers); // [2, 4, 6, 8, 10]
In the example above, we first created an array of numbers. Then, we used the Array.from
method along with the Map
method to create a new array of doubled numbers. The Array.from
method creates a new, shallow-copied Array instance from an array-like or iterable object. The Map
method takes each element in the numbers
array, doubles it, and returns a new array with the doubled values.
You can use the same approach to perform any operation you want on each element of an array. Just replace the function passed to the Map
method with the desired operation.
+ There are no comments
Add yours