This question already has answers here:
Copy array by value
(39 answers)
Closed 6 years ago.
I'm working on a larger project, and I've encountered trouble with arrays, demonstrated below.
var x = new Array();
x = [5, 2];
function doStuff(a){
a[0]++;
console.log(a);//Prints [6, 2]
}
doStuff(x);
console.log(x);//Prints [6, 2] when it should print [5, 2]
How could I do things with an array passed to a function without modifying the original?
What you're passing to doStuff is a reference to the array. You're not actually passing the data.
You're going to have to explicitly copy the array, in order not to modify the source:
var x = [5, 2]; // No need to use the `Array` constructor.
function doStuff(a) {
var x = a.slice(0); // Copy the array.
x[0]++;
console.log(x); // Prints [6, 2]
}
doStuff(x);
console.log(x); // Prints [5, 2]
Related
This question already has answers here:
Why can I change a constant object in javascript
(12 answers)
Closed 1 year ago.
I'm currently learning js and stumbled upon this to me weird behavior. I probably missing something fundamental here, but i can't find the answer.
I have a function that modifies an array and returns it. If i print that out the original array gets modified, even though it's a constant.
const arr = [5, 9, 7, 1, 8, ];
function test(arr) {
const strings = ['qwe', 'asd'];
arr.splice(1, 0, ...strings);
return arr
}
console.log(test(arr))
console.log(arr)
console.log(test(arr))
console.log(arr)
console.log(test(arr))
console.log(arr)
Output shows that the original array gets bigger everytime. I was expecting that it is the same output everytime.
A const means you cannot reassign to it like this
const arr = [1, 2, 3];
arr = [5, 4]; // You cannot do this that's const
other than that you can modify the array contents
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 4 years ago.
Why
let a = [1, 2, 3];
console.log(a === [1, 2, 3]);
is "false" in JavaScript?
Javascript Objects are a bit like C pointers.
a contains the memory address of the first array you define.
When you do console.log(a === [1, 2, 3]); you are in fact creating a new array, and you compare its memory value with the one you kept in a.
That's why:
const a = [];
a.push(1)
is valid (the constant is the "pointer", not the array)
Because the arrays aren't the same. The variable a contains a different array than the one in the console.log even though the array values are the same. You would have to loop through both arrays and compare each value of both to determine if they were equal.
This question already has answers here:
Javascript - insert an array inside another array
(11 answers)
Closed 5 years ago.
I have two arrays:
a = [1,2,3]
b = [4,5,6]
I'd like to insert b at index 1 of a, to have :
c = [1,4,5,6,2,3]
Is there a builtin function to do this ?
I found the answer for a single element, but not for a whole array.
I imagine something like concat but with an additional parameter which would be the index of insertion.
Use Array#splice method.
a = [1, 2, 3]
b = [4, 5, 6]
// copy array a
c = a.slice();
// provide array of arguments using apply method
// and insert elements using splice method
[].splice.apply(c, [1, 0].concat(b))
console.log(c);
var a = [1,2,3],
b = [4,5,6];
a.splice(1, 0, ...b);
console.log(a);
This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 7 years ago.
var a = [1, 2, 3];
var b = a; // b = [1, 2, 3];
console.log(a); // 1, 2, 3
console.log(b); // 1, 2, 3
b.splice(0, 1);
console.log(b); // 2, 3
console.log(a); // 2, 3 <----------- WHY?
I just needed to copy my oryginal "a" array because I want to stay it as [1, 2, 3] forever. How to pop first element from "b" array without touching the oryginal one? Thanks
Your code just needs one small fix:
var a = [1, 2, 3];
var b = a.slice();
I'm not sure of the specifics, but when you are assigning arrays or objects to another variable, the array/object is not copied by value, but rather by reference. .slice method duplicates all elements in the array to the new element rather than just saving a reference to the old one.
This question already has an answer here:
Undestanding double bracket operation in Javascript
(1 answer)
Closed 8 years ago.
I have come accross this strange use if javascript array
Can anyone explain me this.
[1,2,3][1,2]
What type of operation is this ?
In the expression
var result = [1, 2, 3][1, 2]
The first part [1, 2, 3] is an array literal, the second part [1, 2] is an index into the array. So it's equivalent to:
var array = [1, 2, 3];
var index = (1, 2);
var result = array[index];
(1, 2) is a comma expression. It evaluates its arguments from left to right, and returns the value of the last one, so it's equivalent to just 2. So the whole thing is equivalent to:
var result = array[2];
which sets result to 3.