1

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;
VisioN
  • 132,029
  • 27
  • 254
  • 262
thormayer
  • 1,020
  • 5
  • 27
  • 47

7 Answers7

7

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.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • @thormayer: Good deal, glad that helped. I just expanded it slightly, having seen your edit to the question. Best, – T.J. Crowder Jun 19 '12 at 14:31
  • @T.J.Crowder Damn! How can you write that fast! – VisioN Jun 19 '12 at 14:34
  • @VisioN: LOL -- Possibly the best advice my mother gave me when I was in high school was to take typing. She said "You're into computers, so you may as well know how to touch-type." I took her advaice, and there we are, it's been serving me well for over 25 years... :-) – T.J. Crowder Jun 19 '12 at 14:36
3

instead of doing this thing use below code

var arr = [];

arr.push(detailobj);
VisioN
  • 132,029
  • 27
  • 254
  • 262
Ajay Beniwal
  • 18,041
  • 8
  • 72
  • 91
2

You can use object-oriented approach:

var detail = function(id, text) {
    this.id = id;
    this.text = text;
};

​var arr = [];
arr.push(new detail("myid", "mytext"));

console.log(arr);
VisioN
  • 132,029
  • 27
  • 254
  • 262
0

I want to stack them within an array, can I do that ?

Yes you can do that. Array in JavaScript are flexible and can hold objects as well.

Example:

var myArr = [];
myArr.push(detailObj);
Sarfraz
  • 355,543
  • 70
  • 511
  • 562
0

for example:

var idArray = [detailObj1, detailObj2];
Tom
  • 4,046
  • 2
  • 21
  • 38
0

Another one :

var myArray = new Array();
myArray.push(<your object>)
frictionlesspulley
  • 8,030
  • 12
  • 59
  • 102
  • 1
    while it is good to know that the `new Array` constructor exists, i would never recommend using it over `[]`. – jbabey Jun 19 '12 at 14:29
0
detailObj = {

    textDetail: "hello this is my text!",
    id: "tooltip_businessDetails"

};

var array = [];
for(var key in detailObj) {
   array.push(detailObj[key]);
}
alert(array[0]);
Christian Benseler
  • 7,216
  • 6
  • 33
  • 58