Saturday, 17 August 2013

Two for loops one ascending one descending creates two different data structures

Two for loops one ascending one descending creates two different data
structures

I have an array that I am looping twice, one im looping from pointer 0 and
up and the other im looping backwards.
But they produce different array layouts in console.log which i find
strange .. this is what i have:
//array has a length of 3 [0,1,2]
var data = [],
data2 = [];
for(var i = 0; i < array.length-1; i++){
data[i] = {};
data[i].test = 'Hello';
}
for(var i = array.length - 1; i > 0; i--){
data2[i] = {};
data2[i].test = 'Hello';
}
console.log(data);
console.log(data2);
Now in my console log they come out different like this:

So why does the second one have a length of 3 but only 2 objects? And why
does the second console.log have numbers in front of the objects unlike
the first one ?

No comments:

Post a Comment