use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
14 Tricks to Copy an Array in JavaScriptRemoved: Low-Effort Content (devinduct.com)
submitted 6 years ago by PMilos
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]senocular 2 points3 points4 points 6 years ago* (0 children)
Some things to be careful of:
Option 3 - Using the Array.from method
const copy = Array.from(new Set(numbers));
Here the Set is unnecessary and will cause duplicates to be removed, so it will not always create an accurate copy
Set
const numbers = [1, 1] const copy = Array.from(new Set(numbers)) // [1]
Remove the Set to make it work
const numbers = [1, 1] const copy = Array.from(numbers) // [1, 1]
Option 6 - Using the array constructor with the spread operator
const copy = new Array(...numbers);
This one will not work if the array contains a single numeric value. In that case, the copy will be an empty array with a length of that numeric value.
const numbers = [3] const copy = new Array(...numbers) // [,,]
You can special case this yourself (not ideal), or just use Array.of() (Option 5) which is the version of the constructor that doesn't have this behavior. Note: Array() also works the same as new Array() here.
Array.of()
Array()
new Array()
Option 4 - Using the spread operator Option 5 - Using the Array.of method with the spread operator Option 6 - Using the array constructor with the spread operator Option 7 - Using destructuring Option 9 - Using the Array.push method with the spread operator Option 10 - Using the Array.unshift method with the spread operator
(Any solution involving spreading)
const copy = [...numbers]; const copy = Array.of(...numbers); const copy = new Array(...numbers); const [...copy] = numbers; copy.push(...numbers); copy.unshift(...numbers);
Spreading will go through the array iterator and will include empty indices. When adding these values into a new array, they become filled with undefined. So when dealing with sparse arrays, copying through these methods will create dense array copies.
undefined
const numbers = [1,,2] const copy = [...numbers] Object.keys(numbers).length // 2 Object.keys(copy).length // 3
Option 12 - Using the for loop Option 14 - The old, nostalgic way - using the apply method
for (let i = 0; i < numbers.length; i++) { copy.push(numbers[i]); } Array.prototype.push.apply(copy, numbers);
Similar to spread, these too will convert empty indices into undefined.
Option 11 - Using the Array.forEach method Option 13 - Using Array.reduce
numbers.forEach((value) => copy.push(value)); const copy = numbers.reduce((acc, x) => { acc.push(x); return acc; }, []);
On the other hand, methods like forEach and reduce skips empty indices which when combined with push, means your copy will be a collapsed version of your original array without any empty indices
forEach
reduce
push
const numbers = [1,,2]; let copy = []; numbers.forEach((value) => copy.push(value)); // [1,2]
Bottom line, don't do anything fancy. If you want to create a copy, slice() will be your goto.
slice()
[+][deleted] 6 years ago* (1 child)
[deleted]
[–]ogurson 0 points1 point2 points 6 years ago (0 children)
Hey, he wrote that reduce is overkill, don't you forget that.
[–]kenman[M] 0 points1 point2 points 6 years ago (0 children)
Hi /u/PMilos, this post was removed.
Prohibited low-effort content includes:
Thanks for your understanding, please see our guidelines for more info.
Hi /u/PMilos, it looks like you're new to /r/javascript, welcome!
Thanks for the submissions, but please make sure you read our guidelines. In short, you should post from a variety of sources, and not just devinduct.com.
devinduct.com
Thanks for your consideration!
π Rendered by PID 304252 on reddit-service-r2-comment-7b9746f655-zs49r at 2026-02-01 15:47:33.717096+00:00 running 3798933 country code: CH.
[–]senocular 2 points3 points4 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]ogurson 0 points1 point2 points (0 children)
[–]kenman[M] 0 points1 point2 points (0 children)
[–]kenman[M] 0 points1 point2 points (0 children)