To copy values from an array in JavaScript

Asked 2 years ago, Updated 2 years ago, 45 views

var arr1 = ['a','b','c'];
var arr2 = arr1;
arr2.push('d');  // arr1 = ['a','b','c','d']

As you can see from the example above, if you put arr1 in arr2, it doesn't copy the value to arr2, but arr2 becomes another arr1. That's not what I want, but I want to copy the value in arr1 to arr2. So that the two are independent arrangements. How can we do that?

javascript array

2022-09-21 21:13

1 Answers

in a very simple way var newArray = oldArray.slice(); You can do it in this way.


2022-09-21 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.