This question already has answers here:
Copy array by value
(39 answers)
Closed 6 years ago.
Given this two arrays a and b:
var a = [1,2,3];
var b = a;
a.push(4);
console.log(b); /* [1,2,3,4] */
console.log(a); /* [1,2,3,4] */
Why isn't b equal with [1,2,3] ?
The variable b holds the reference to array a. You need to copy the array instead use Array#slice method to copy.
var a = [1, 2, 3];
var b = a.slice();
a.push(4);
console.log(b);
console.log(a);
Related
This question already has answers here:
Zip arrays in JavaScript?
(5 answers)
Closed 3 years ago.
I wanted to merge two arrays like you can fold 2 packs of cards - in ugly code with for loop and assuming same length so no if's for safety it would look like this:
const arr = [1,2,3];
const rra = [4,5,6];
const result = [];
for(let i=0; i<arr.length; i++){
result.push(arr[i],rra[i]);
}
console.log(result); // Array(6) [ 1, 4, 2, 5, 3, 6 ]
I know there is something similar in String.raw() but it cuts off last element and returns a string, is there equivalent in array ?
String.raw({raw: [1,2,3]}, ...[4,5,6]); //"14253"
You can use .flatMap() for this - This is the same as .map(), followed by .flat()
const arr = [1,2,3];
const rra = [4,5,6];
let newArray = arr.flatMap((ele, i) => [ele, rra[i]]);
console.log(newArray);
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:
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]
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.