How not to use arrays in JavaScript
by alexbfree
JavaScript
b = [];
standardProperties = Object.keys(b).length;
obj1 = obj2 = obj3 = obj4 = { 'data':'some data'};
b["key1"] = obj1;
b["key2"] = obj2;
b.push({"key3" : obj4});
b.push({"key4" : obj4});
console.log("\n\narray reports length "+b.length);
propertiesThatIAdded = Object.keys(b).length - standardProperties;
console.log ('\n\narray has '+b.length+' new elements:');
for (var i = 0; i < b.length; i++) {
console.log (i+":");
console.dir(b[i]);
}
console.log ('\n\narray has '+propertiesThatIAdded+' new properties:');
for(var propertyName in b) {
console.log(propertyName+":");
console.dir(b[propertyName]);
}
console.log('\n\narray indexes as follows:');
console.log('arr[key1]:');
console.dir(b["key1"]);
console.log('arr[key2]:');
console.dir(b["key2"]);
console.log('arr[key3]:'+b["key3"]);
console.log('arr[key4]:'+b["key4"]);
console.log('arr.key1]:');
console.dir(b.key1);
console.log('arr.key2]:');
console.dir(b.key2);
console.log('arr.key3]:'+b.key3);
console.log('arr.key4]:'+b.key4);
console.log ('\n\ncomplete array contents:');
console.dir (b);