Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

holes in arrays vs. undefined and the map function

I understand that map is not called on undefined indexes on arrays and I appreciate that an undefined index is different from an array index explicitly assigned the 'undefined' value (it is, isn't it?). Yet, how is one supposed to distinguish between holes in an array and undefined values?

The below code:

foo.js

var arr = [,,undefined,,,,3,,,4];
console.log("A hole the same as undefined? "+(arr[0]===undefined?"yes":"no"));
var _ignored = arr.map(function(x) {console.log("f called on ["+x+"]");});  

... produces when running:

$ node foo.js 
A hole the same as undefined? yes
f called on [undefined]
f called on [3]
f called on [4]

... similar results are to be had when replacing map with forEach.

like image 695
Marcus Junius Brutus Avatar asked Feb 14 '15 16:02

Marcus Junius Brutus


People also ask

What is the difference between a map and an array?

A map is an associative container that stores elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have equal key values. The diagrammatic representation of Map is given below: Integer>> itr = gquiz1.entrySet (). An Array is a collection of elements of the same data type.

How do you find the hole in an array?

Holes in Arrays # Holes are indices “inside” an Array that have no associated element. In other words: An Array arr is said to have a hole at index i if: 0 ≤ i < arr.length

How are holes treated in array methods in ES6?

For more information, consult Sect. “ Holes in Arrays ” in “Speaking JavaScript”. The general rule for Array methods that are new in ES6 is: each hole is treated as if it were the element undefined. Examples:

Are there any rules for holes in arrays?

With regard to holes in Arrays, the only rule is now that there are no rules. Therefore, you should avoid holes if you can (they affect performance negatively, too). If you can’t then the table in the previous section may help. Hi, thanks for this great post.


1 Answers

The in operator tells you if an index has been actually assigned to:

a = []
a[5] = window.foo

document.write(a[4] + "<br>")
document.write(a[5] + "<br>")

document.write((4 in a) + "<br>")
document.write((5 in a) + "<br>")

Javascript arrays are actually objects with a special length property, and array indexes are just property names (in fact, they are even strings, not numbers, internally). So the above definition is equivalent to:

a = {
  5: window.foo,
  length: 6
}

Therefore, all object functionality related to keys (like in, hasOwnProperty, Object.keys) work for array indexes as well.

forEach and other iteration methods work by iterating from 0 to length-1 and checking if n-th index is actually present in the argument, they don't "know" if the argument is actually an array or just a generic object:

a = {1:'one', 5:'five', length:100};

[].forEach.call(a, function(x) {
  document.write(x)
});
like image 90
georg Avatar answered Oct 10 '22 22:10

georg