.splice() - how to protect oryginal array? [duplicate] - javascript

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.

Related

Javascript beginner - function modifiing global const [duplicate]

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

How to subtract one array from another, in javascript [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
If I have an array A = [1, 2, 3, 4, 5] and B = [3, 4, 5] I want to return a new array with values
[1, 2]. remove same value.
Using Array.prototype.includes, you can check if B contains A item or not.
And using Array.prototype.filter, you can get the filtered values which are not included in array B.
const A = [1, 2, 3, 4, 5];
const B = [3, 4, 5];
const output = A.filter((item) => !B.includes(item));
console.log(output);

Why "let a = [1, 2, 3]; console.log(a === [1, 2, 3]);" is "false" in JavaScript? [duplicate]

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.

Javascript: insert array in another array at a specific index [duplicate]

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);

JS Array Overwritten in Function [duplicate]

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]

Categories