What does `empty` mean in a JS array? [duplicate] - javascript

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").

Related

Filter Out Element from Nested Array [duplicate]

This question already has answers here:
Remove all falsy values from an array
(26 answers)
How to remove false values from array?
(6 answers)
Closed 3 days ago.
let input array=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let output array=[["1.81","2.24"],["5.62","6.26"],["2.31","1.64"]];
I have a nested input array which contains smaller arrays and false statement as shown in the console. How do I remove the false statement from the input array? I have tried using for loop to loop through all the 6 elements to check each element with a (if !==false), then push into a new array called the output array but I could not get it to work? May I know how to solve this? Your help will be very much appreciated :)
Directly use Array#filter:
let input=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let res = input.filter(Boolean);
console.log(res);

How to determine if an array includes another array in JS? [duplicate]

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

Syntax for multidimensional arrays - x[?,?] and x[?][?] [duplicate]

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.

Javascript Array Equality Fails [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 6 years ago.
I've got two lists. When I run array1 == array2, the console prints false. If I iterate through them and check equality for each item, it prints true every time. What's wrong?
for (var i=0; i<array1.length; i++) {
console.log(array1[i] == array2[i]);
}
From the Safari console:
All my google searches turned up things about array diffs and checking equality of unordered arrays. I thought there must be an easier way to solve the problem of two lists in the same order, and I couldn't find that online.
use array1[i].equals(array2[i]) Like it was stated in the comments you are not comparing the contents with the ==.

Why does Array.map not seem to works on an Array of undefined values [duplicate]

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]

Categories

Resources