- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create new array without impacting values from old array in JavaScript?
Let’s say the following is our current array −
var listOfNames=["John","Mike","Sam","Carol"];
Use JSON.parse(JSON.stringify()) to create new array and set values from the old array above.
Example
function createNewArray(listOfNames) { return JSON.parse(JSON.stringify(listOfNames)); } var listOfNames=["John","Mike","Sam","Carol"]; var namesArray = listOfNames.slice(); console.log("The new Array="); console.log(namesArray);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo111.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo111.js The new Array= [ 'John', 'Mike', 'Sam', 'Carol' ]
- Related Articles
- Mapping an array to a new array with default values in JavaScript
- Create a new array from the masked array and return a new reference in Numpy
- Sum of array object property values in new array of objects in JavaScript
- Map to create new value from int array in Java
- Create array from JSON object JavaScript
- JavaScript: How to map array values without using "map" method?
- Select random values from an array in JavaScript?
- Extract unique values from an array - JavaScript
- Remove same values from array containing multiple values JavaScript
- Accumulating array elements to form new array in JavaScript
- Pulling data from array into new independent variables JavaScript?
- When summing values from 2 arrays how can I cap the value in the new JavaScript array?
- How to create an ordered array from values that have an order number in JavaScript?
- Sum from array values with similar key in JavaScript
- Fetch specific values from array of objects in JavaScript?

Advertisements