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
Related
This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 1 year ago.
so basically i found out that if we create array for example arr1 = [1, 2, 3] and new const { length } = arr1; will going to output 3 but how is this works exactly? is there any more operators instead of { length }? and what are the other use of using const { here some stuff } syntax? i do some research but i didnt find anything that points out this syntax. any assumptions?
Basically this is Object destructuring
Basic example:
const user = {
id: 42,
is_verified: true
};
const {id, is_verified} = user;
console.log(id); // 42
console.log(is_verified); // true
In your case:
const arr1 = [1, 2, 3]
arr1.length = 3
// Or,
const { length } = arr1
To know more: Object destructuring
Let examine each line of code :)
const arr1 = [1,2,3];
Here, you define an array "arr1", which has 3 elements, so arr1's length is 3.
In javascript, arr1 is an object, and 1 of its properties is "length", which reflects the length of the array. So, arr1.length = 3
Now the second line :
const {length} = arr1
Here, we use destructuring assignment to get the property "length" from object arr1. This line is equal to :
const length = arr1.length;
which give you length = 3
Make sense?
You can read more about the destructuring assignment here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
This question already has answers here:
to remove first and last element in array
(14 answers)
Closed 3 years ago.
const myOldArr = [2, 3, 6];
const newArr = myOldArr.pop().shift();
Why does doing this give me a TypeError? Is there a way to do this without a loop? I want the new array to be stored inside a variable so the remaining numbers that don't get removed are stored inside an array of some sort.
.pop() returns the last element form your array and so .shift() won't work,
If you want to keep the myOldArr and create a new one without the first and last element, you can do this by using .slice(1, -1):
const myOldArr = [2, 3, 6];
const myNewArr = myOldArr.slice(1, -1);
console.log(myNewArr);
Or, if you don't mind moifying the original array, you can do this by not chaining both methods:
const myOldArr = [2, 3, 6];
myOldArr.pop()
myOldArr.shift();
console.log(myOldArr);
Use them separately
Array.pop() and Array.shift() will return a value of array
your trying to use shift in number 6 so it will throw error
const myOldArr = [2, 3, 6];
myOldArr.pop();//last
myOldArr.shift();//first
console.log(myOldArr)
This question already has answers here:
Summing ; delimited values in Javascript
(1 answer)
How to sum elements at the same index in array of arrays into a single array?
(7 answers)
Closed 5 years ago.
I am trying to add together an undetermined amount of arrays, but only add or reduce them item by item.
I cannot figure out how to make this work in ES6 javascript.
const arrayList = [
[1, 2, 3],
[1, 2, 3],
[2, 2, 2]
];
const totalRow = arrayList.map((array) => {
const total = [];
array.map((number, i) => {
total[i] = (total[i] == undefined ? 0 : total[i]) + number;
});
return total;
});
//Output is just 3 more arrays with no change, as it seems
//the total variable resets to an empty array with each iteration.
//what i want is this output:
[4, 6, 8]
What am I doing wrong? I tried reduce method as well but came up empty
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.