This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 5 years ago.
I'm using https://js.do/ as a sandbox for simple scripts and document.write to print results. I'm working with multidimensional arrays, with code such as
var x = [];
x[1, 2] = 0;
However, I'm a little confused on what exactly document.write is printing.
On researching multidimensional arrays in JS, I found no mention of the notation used above, but rather x[1][2] was used in the examples found instead (ie. an array inside of an array).
I can't remember where I first came across the above way of using multidimensional arrays - perhaps someone could provide a link to enlighten me?
x[1, 2] = 0; assigns 0 to index 2 of x, where comma operator , evaluates last value of 0, 2 expressions as an index of x at bracket notation
var x = [];
x[1, 2] = 0;
console.log(x[2] === 0);
The syntax with the comma is incorrect, but apparently doesn't cause a syntax error. It is being interpreted by simply taking the last value, so the example x[1,2] = 0 is being views as x[2] = 0. That creates an array with 0 in the third position (index 2), [undefined,undefined,0]. As that gets written to the DOM, the undefined is ignored, but a comma is still added. So your output is ,,0.
Related
This question already has answers here:
How to get value at a specific index of array In JavaScript?
(8 answers)
Closed 2 years ago.
so I am currently learning javascript on Codecademy. And a weird kind of thing got introduced that I don't quite get.
if you look at the code. you see at the end in console that after logging animals it is a [i] like why is that there? I get that is has something to do with the for loop. But I don't quite understand like why or what that it does. I don't know if question is clear enough but if you just try to explain why it is there and what it does there. that would be greatly appreciated:)
const animals = ['Grizzly Bear', 'Sloth', 'Sea Lion'];
for (let i = 0; i < animals.length; i++){
console.log(animals[i]);
}
the bracket notation is used to specify the index of the array you are looping through. You can always read more about javascript arrays on w3schools
This question already has answers here:
What is "undefined x 1" in JavaScript?
(5 answers)
What's the difference between empty items in a JavaScript array and undefined? [duplicate]
(2 answers)
Closed 3 years ago.
Can anyone help me understand what an empty means in a JS array?
I just noticed an array in chrome devtools with an empty value. I created the one below, and by putting two commas together I can produce an empty (see below). Granted, I'm relatively new to JS, but since empty isn't a JS primitive I would have expected either:
a) a null or undefine3d value for a[3]
b) a.length to be 4
What does empty mean?
Array literals can include elisions, e.g.
[0,1,,3];
The two commas together mean "there is no element at index 2". Many stringified versions will show undefined at index 2, however that is misleading as there is no element at all at that position.
E.g.
let arr = [0,1,,3];
arr.forEach((v, i) => console.log(`index ${i} has value ${v}`));
console.log('stringified: ' + arr);
console.log('SO console:', arr);
console.log('JSON: ' + JSON.stringify(arr));
An alternative is to show "empty", which (IMHO) isn't really suitable either, it should be shown as in the literal (i.e. as for "stringified").
This question already has answers here:
Why Array.indexOf doesn't find identical looking objects
(8 answers)
Closed 3 years ago.
I'm wondering what the best way to determine the membership of one array in another array in JS.
Here's an example
let a = [];
a.push([1,2]);
a.includes([1,2]) <- evaluates to false
a.indexOf([1,2]) <- evaluates to -1
What's the deal here? Any efficient work around?
At the moment, your search array doesn't actually equal the array within your a array as they have 2 different references in memory. However, you could convert your arrays to strings, such that your search can equal another string array within your array.
To do this you could convert your inner arrays to string using .map(JSON.stringify) and then search for the string version of your array using .includes(JSON.stringify(search_arrr)).
See example below:
let a = [];
let search = [1, 2];
a.push([1,2]);
a = a.map(JSON.stringify)
console.log(a.includes(JSON.stringify(search)));
This question already has answers here:
JavaScript "new Array(n)" and "Array.prototype.map" weirdness
(14 answers)
Closed 8 years ago.
This is purely a flexibility exercise with JavaScript. I'm attempting to create an array of integers with Javascript without a loop and in this particular fashion:
var a = Array(100).map(function(x, y) { return y + 1 });
I would expect and Array of integers 1 - 100. Instead it remains undefined * 100? I can't even console.log from within the map function?
I understand from this great post Sequences using JavaScript Array that the below does accomplish the goal using Array.apply, I just understand why its needed?
var a = Array.apply(0, Array(100)).map(function(x,y) { return y + 1 }); a
Thanks for any insight :)
It's mainly for memory space optimization purpose, sparse arrays take less space in memory.
It kinds of "flags" with undefined so that you can do this kind of stuff without exploding memory space:
var a = [];
a[2014] = 0;
console.log(a.length); // 2015
console.log(a); // [undefined x 2014, 0]
This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 8 years ago.
Using Javascript in Firefox, Safari and Chrome (didn't try IE), the following strange behavior happens:
data = new Array(1,3,5,7,9);
data[1,7,3] = 88;
alert( data ); // displays: 1,3,5,88,9
So apparently the value at index [3] has been changed (other tests show that it actually has changed data[3]).
The second command does not generate an error. It does not even generate a warning in NetBeans, nor an error when displayed in a browser.
I explored this a bit further. It appears that :
data[1,7,null,NaN,4,3]
also gets interpreted as data[3] -
this also works with other values than 3.
The last value in the list is used and the rest are ignored.
Does this behavior have some sort of meaning or purpose, or is it just an unexpected fault in the parser?
I was unable to find any documentation or explanation of this behavior/syntax.
You're using the comma operator.
<expr1>, <expr2>, <expr3>, ...
is an expression that evaluates each expression from left to right, and returns the value of the last one. So 1,7,3 evaluates to 3. So
data[1,7,3] = 88;
is equivalent to:
data[3] = 88;