There are several ways to copy an array in JavaScript:
- Using the spread operator (…): This creates a new array with the elements of the original array. Example: let newArray = […originalArray];
- Using the slice method: This creates a new array with the elements of the original array. Example: let newArray = originalArray.slice();
- Using the Array.from() method: This creates a new array with the elements of the original array. Example: let newArray = Array.from(originalArray);
- Using the concat() method: This creates a new array with the elements of the original array. Example: let newArray = [].concat(originalArray);
- Using the map() method: This creates a new array with the elements of the original array. Example: let newArray = originalArray.map(x => x);
Please note that, all the above methods will copy the array as shallow copy, which means if the array has any object or array as element, the reference of that object will be shared across the both arrays.
Here are some examples of copying an array in JavaScript using the different methods I mentioned earlier:
Using the spread operator:
let originalArray = [1, 2, 3, 4, 5];
let newArray = [...originalArray];
console.log(newArray); // [1, 2, 3, 4, 5]
Using the slice method:
let originalArray = [1, 2, 3, 4, 5];
let newArray = originalArray.slice();
console.log(newArray); // [1, 2, 3, 4, 5]
Using the Array.from() method:
let originalArray = [1, 2, 3, 4, 5];
let newArray = Array.from(originalArray);
console.log(newArray); // [1, 2, 3, 4, 5]
Using the concat() method:
let originalArray = [1, 2, 3, 4, 5];
let newArray = [].concat(originalArray);
console.log(newArray); // [1, 2, 3, 4, 5]
Using the map() method:
let originalArray = [1, 2, 3, 4, 5];
let newArray = originalArray.map(x => x);
console.log(newArray); // [1, 2, 3, 4, 5]
For deep copy you can use JSON.parse(JSON.stringify(originalArray))
let originalArray = [1, 2, {a:1, b:2}];
let newArray = JSON.parse(JSON.stringify(originalArray));
console.log(newArray);
Please let me know if you have any question or need more examples.
Also Read:
- How To Get The Last Character Of A String In JavaScript
- Remove The Last Character Of A String In JavaScript
- How To Validate An Email Address In JavaScript
- How To Check If An Input Field Is Empty In JavaScript
- Check If An Input Field Is A Number In JavaScript
- Confirm Password Validation In JavaScript
- How To Print A PDF File Using JavaScript
- Calculate The Number Of Days Between Two Dates In JavaScript
- How To Compare Two Dates In JavaScript
- Calculate Age With Birth Date YYYYMMDD In JavaScript
- How To Append or Add Text To A DIV Using JavaScript
- How To Get The Text Of HTML Element In JavaScript
- How To Change The Text Inside A DIV Element In JavaScript
- Show/Hide Multiple DIVs In JavaScript
- Show A DIV After X Seconds In JavaScript
- Display A JavaScript Variable In An HTML Page
- How To Generate A Random Number In JavaScript
- Bubble Sort In JavaScript
- Insertion Sort In JavaScript
- Selection Sort In JavaScript
- How To Remove A Specific Item From An Array In JavaScript
- Merge Sort In JavaScript
- Round To 2 Decimal Places In JavaScript
- SetInterval() and setTimeout() Methods In JavaScript
- Generate A Unique ID In JavaScript
- Caesar Cipher In JavaScript
- How To Reverse A String In JavaScript
- How To Loop Through A Plain JavaScript Object
- How To Open A URL In A New Tab Using JavaScript?