0

For example, I create constructor and initialize array of objects (JS)

function SomeConstruct(val1, val2) {
    this.val1= val1;
    this.val2= val2;
}


var someCons= new Array();
someCons[0] = new SomeConstruct(12, 40);
someCons[1] = new SomeConstruct(34, 42);
someCons[2] = new SomeConstruct(0,-5);

And then I create new object with index [5] though last index of array is [2]

someCons[5]=new SomeConstruct(43,232);

But this code works, but when I try to access objects [3] or [4] I get error

How can I prevent this behavior - for instance I have some loop where I add new objects to the array with some condition ( val1==1) but at some point this logic breaks and I miss some index of array as above and create [5] instead of [3] element ? Why does JS allows this kind of behavior ?

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
VolodymyrNaz
  • 33
  • 1
  • 7
  • Any string will serve as a property name for a JavaScript object. – Pointy Apr 24 '15 at 14:05
  • I believe you will find your answer here: [SO](http://stackoverflow.com/questions/4771001/how-best-to-do-a-javascript-array-with-non-consecutive-indexes) In short, use an object instead of an array if you want and the you can use the for-in enumeration to get the data back out. – JasonWilczak Apr 24 '15 at 14:07

4 Answers4

1

How can I prevent this behavior

Use the push method to add an element to an array, instead of assigning to a fixed index that might be beyond the actual array.

Why does JS allows this kind of behavior?

Because arrays in JS are sparse. After all, you could always have an undefined value in array of objects. If you really have to deal with them, you will need to jump over the missing indices in your iteration (forEach does that as well).

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

an alternative to using the push methods above is -

someCons[someCons.length] = new SomeConstruct(val, val);  

which does the same thing - adding the new construct to the current last position in the array.

Craicerjack
  • 6,203
  • 2
  • 31
  • 39
0

In the JavaScript if you assign value to some random index of Array the following behavior, I tried this code on browser console :

a = new Array(10);

[undefined × 10]

a = new Array()

[]

a.length

0

a[10] = "a"

"a"

a[1]="b"

"b"

a

[undefined × 1, "b", undefined × 8, "a"]

a[20]=30;

30

a

[undefined × 1, "b", undefined × 8, "a", undefined × 9, 30]

To prevent this you can push your element in the Array like

var arrSomeCons= new Array();
someCons = new SomeConstruct(12, 40);
arrSomeCons.push(someCons)
Rakesh Chouhan
  • 1,196
  • 12
  • 28
0

In your case, when the last index of array is [2] and you create new object in [5], [3] and [4] become undefined, because you didn't declare them. If you want to do it successively and don't miss any indexes, you can use push, or use code like this:

for(i = 0; i < 10; i++)
  array[i] = new Constr();

or

for(some conditions to declare variables)
   array.push(new Constr(variables));

or

array.push( elem1, elem2, ... )