I have a perl hash which I'm looping over and building a JavaScript array. The JavaScript array starts out with a length of 0 when I initiate it; however, it quickly grows to 1001 in the first past, 2001 in the second, and 4001 in the third pass. I'm expecting the length to be 3! Here's the code and the perl hash following.
Code
var offers = [];
% foreach my $amount (keys %$offers) {
offers['<% $amount %>'] = [];
console.log(offers.length);
% }
Perl Hash
{
'1000'=>{
'6'=>{
'payment'=>'173.49',
'fee'=>'2',
'APR'=>'13.9'
},
'4'=>{
'payment'=>'256.23',
'fee'=>'2',
'APR'=>'11.9'
}
},
'2000'=>{
'6'=>{
'payment'=>'346.98',
'fee'=>'2',
'APR'=>'13.9'
},
'4'=>{
'payment'=>'512.46',
'fee'=>'2',
'APR'=>'11.9'
}
},
'4000'=>{
'6'=>{
'payment'=>'693.96',
'fee'=>'2',
'APR'=>'13.9'
},
'4'=>{
'payment'=>'1024.92',
'fee'=>'2',
'APR'=>'11.9'
}
}
};
var indices; for(key in array){indices.push(key)} console.log(indices.length)offers[1000] = []. That creates an array whose length is 1001, since it consists of indices 0 through 1000. Maybe you should be using an object instead of an array.