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.
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:
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:
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:
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]