I have lots of details. I want to stack them within an array, can I do that?
detailObj = {
textDetail: "hello this is my text!",
id: "tooltip_businessDetails"
};
var idArray[0] = detailObg;
Yes:
var idArray = [
{
textDetail: "hello this is my text!",
id: "tooltip_businessDetails"
},
{
textDetail: "another",
id: "anotherid"
},
// ...and so on
];
Or if you already have variables pointing at them:
var idArray = [detailObj, anotherDetailObj, yetAnotherDetailObj];
You can also do it dynamically after array creation:
var idArray = [];
idArray.push(detailObj);
idArray.push({
textDetail: "another",
id: "anotherid"
});
idArray.push(yetAnotheretailObj);
And you can do it the way you tried to, just with a slight change:
var idArray = [];
idArray[0] = detailObj;
idArray[1] = anotherDetailObj;
Any time you set an array element, the array's length property is updated to be one greater than the element you set if it isn't already (so if you set idArray[2], length becomes 3).
Note that JavaScript arrays are sparse, so you can assign to idArray[52] without creating entries for 0 through 51. In fact, JavaScript arrays aren't really arrays at all.
Another one :
var myArray = new Array();
myArray.push(<your object>)
new Array constructor exists, i would never recommend using it over [].
var idArray[0] = detailObj;.