well, to remove those undefined
parts do
a[0] = 'A';
a[1] = 'B';
In your snippet, you fill the element with index 10
which forces the ECMAscript to create an array with 10 fields
. There are no definitions for all that fields between 1
and 10
, which means those are correctly undefined
.
To remove that fields you either have to set a proper value or map
the non-undefined
values into a new array which would just be unnecesarry if you create a correct array in the first place.
Create an true object
instead of an array (which actually also is an object) to have your desired behavior.
var a = {};
a[1] = 'A';
a[2] = 'B';
console.log(a);