magnify
magnify
Home JavaScript More undefined than undefined in JavaScript arrays
category

More undefined than undefined in JavaScript arrays

Published on November 13, 2015 by in JavaScript

Working on an node/express API, writing some unit tests, I discovered something I had no clue about…

Everything started from this piece of code:

    (new Array(100)).forEach(function (none, index) {
      var userX = new models.User({
        lastname: 'lastname' + index,
        firstname: 'firstname' + index,
        avatar: mongoose.Types.ObjectId().toString()
      });

      // ... some other stuff ...

      promises.push(userX.save());
    });

Idea in this piece of code was to produce 100 users using a closure with an index.

It didn’t worked (as you can imagine, else, this article wouldn’t have been written 🙂 )…

After a few searches, I got the reason… forEach skips undefined “items”.

Till there, ok, I didn’t know but ok…

But, wait…

> var a = ["w", undefined, "f"];
undefined
> a.forEach(function(item, index) {console.log(index, item)});
0 'w'
1 undefined
2 'f'

I was sure of this comportment, so, what is going on when using delete

> var a = ["w", "t", "f"];
undefined
> delete a[1]
true
> a
[ 'w', , 'f' ]
> a.forEach(function(item, index) {console.log(index, item)});
0 'w'
2 'f'

In fact, array are objects with keys, and delete also removes the key:

> var a = ["w", undefined, "f"];
undefined
> Object.keys(a)
[ '0', '1', '2' ]
> var a = ["w", "t", "f"]
undefined
> delete a[1]
true
> Object.keys(a)
[ '0', '2' ]

Finally, my mistake was to suppose that, creating an array fixing a length also presets its keys.

And to correct my firth hypothesis, forEach doesn’t loop on undefined keys which makes sense 🙂

Here is a funny resume:

> var tab = new Array(100);
undefined
> tab
[undefined × 100]
> Object.keys(tab)
[]
> tab.fill(undefined)
[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
> tab
[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
> Object.keys(tab)
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"]
 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 

You must be logged in to post a comment.