How to usemap() Method in Javascript
JavaScript map method can be used to copy array items into another array. By applying a callback function that returns the original item, a new array is created with copied items,
Syntax:
map(function (element) { /* … */ })
Example: In this example, we are using the map() method to copy an array item into another array.
const arr = [5, 10, 15, 20, 25];
const result = arr.map(item => item);
console.log(result);
Output
[ 5, 10, 15, 20, 25 ]
Copy Array Items into Another Array in JavaScript
We will see how to copy array items into another array in JavaScript. This operation is valuable when you need to generate a new array containing either identical or a subset of elements from an original array.
These are the following ways to solve this problem:
Table of Content
- Using the Spread Operator (…)
- Using slice() Method
- Using apply() Method
- Using Array.concat() Method
- Using push() Method
- Using map() Method
Contact Us