I created an array the usual way: var subtotal = new Array();, but I created elements using an index from another set of data, with several numbers as index, like this:
function Sub(i, amount){
this.index= i;
this.amount= Number(amount);
}
var subtotal= [
new Sub(360, "23.50"),
new Sub(459, "8"),
new Sub(825, "12.50"),
new Sub(15253, "17")
];
var sum= 0;
for(var i= 0, L= subtotal.length;i<L;i++){
sum+= subtotal[i].amount;
}
sum.toFixed(2)
/* returned value: (String)
61.00
*/
var sum = new Number();sum=0;
for (var i=0;i<subtotal.length;i++){
sum=sum+subtotal[i];
}
compare this with:
Code:
for (item in subtototal) {
sum = sum + subtotal[item];
}
Code:
for (item in subtotal) {
console.log(item, subtotal[item]);
}
Output:
360 23.50
459 8
825 12.50
15253 17
That is, JavaScript does not "fill in the gaps". Arrays are objects and the indices are attributes of the (array-)object.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 01-05-2013 at 04:24 PM..
It' always possible to use an object to avoid to define a 15554 length array !
Code:
var subtotal= {};
subtotal[360]= 23.50; // a number without " "!
subtotal[459]= 8;
subtotal[825]= 12.50;
subtotal[15253]= 17;
sum=0;
for (i in subtotal) sum+=subtotal[i];
alert(sum.toFixed(2));
Last edited by 007julien; 01-05-2013 at 10:30 PM..
An array is an object, but then so is a date.. and anything else for that matter
Code:
var subtotal= [];
subtotal[360]= "23.50";
subtotal[459]= "8";
subtotal[825]= "12.50";
subtotal[15253]= "17";
subtotal.Santa = "Claus";
console.log(subtotal.Santa, subtotal['Santa']); // Claus Claus
subtotal.xmas = function () { alert('Ho! Ho!'); };
subtotal.xmas();
var dte = new Date();
dte.season = "Winter";
console.log(dte.season); // Winter
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
It' always possible to use an object to avoid to define a 15554 length array !
The array does not have that number of elements. The .length will be reported as 15554, as this is one more than the largest index, but the actual length - the number of elements - is four - as my previous code confirmed:
Code:
for (item in subtotal) {
console.log(item, subtotal[item]);
}
Output:
360 23.50
459 8
825 12.50
15253 17
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
var newArray = [];
newArray[12] = "whatever";
newArray[50] = "hi";
newArray[34] = "what";
newArray['54'] = "Whoa!";
alert(newArray.length); // 55
alert(newArray['12']); // whatever
for (item in newArray) {
console.log(newArray[item]);
}
Output:
whatever
what
hi
Whoa!
When we define array elements using a number [12] we are creating object-attributes, rather than indices. However, JavaScript realises that it is a number and uses it to re-order (but not necessarily physically..) all those elements that also have a number as attribute. Because JS is (very) loosely typed, it also recognises '12' as being a number, and so would order this element as well.
When we use numbers as the attribute (index) JS compares it with all other numerical-indices, and uses the largest of these numbers plus one as the .length of the object/array.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 01-05-2013 at 11:07 PM..
If something in JavaScript is not an Object then it does not exist.. because everything in JS, apart from primitives, is an instance of Object.
But I recall having this discussion before: I don't think it will lead to a conclusion
[Did IE used to reserve space..? I can't recall.]
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Andrew, I'm not so experienced and didn't know the for (item in subtotal) but that did the trick. And yes, there are only those items in the array, not 15253 or whatever the highest index is, but that makes me think... then how "lenght" works? I always thought the lenght method reported the total of "occupied cells" in an array.
Null is not an object. It exists like subtotal[100] which is undefined !
Code:
alert(typeof null); // object
But this conversation will lead no where: it is undefined.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Andrew, I'm not so experienced and didn't know the for (item in subtotal) but that did the trick. And yes, there are only those items in the array, not 15253 or whatever the highest index is, but that makes me think... then how "lenght" works? I always thought the lenght method reported the total of "occupied cells" in an array.
no, length is the highest index of the array, not the filled slots. this allows for arrays to be pre-allocated. compare with VB's ubound() function.
since js is scripted, this is more of a wink and nod to traditional programming than anything, although it does allow bounds checking, and a very simple and fast way to empty an array by setting length to zero.
null is indeed an object, and it is not undefined. i suggest reading the spec for the gruesome details.
just know that like 0, false, and undefined, Boolean(null) is false.
null doesn't come up very often, but when it does, it's usually used by the DOM to differentiate between something that doesn't exist like document.click (undefined) versus something that's uninitiated, like document.onclick (null)
one trick for counting "populated" elements is by using Object.keys():
Code:
var subtotal= [1,2,3]; // 3 elements
subtotal[360]= "23.50"; // add a fourth element
subtotal[365]= null; // add a fifth element (note that null is not undefined)
Object.keys(subtotal).length // === 5
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%