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;
2
  • What exactly is your question? Commented Jun 19, 2012 at 14:26
  • Your above code has a typo in the last line. probably should be var idArray[0] = detailObj;. Commented Jun 19, 2012 at 14:26

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@thormayer: Good deal, glad that helped. I just expanded it slightly, having seen your edit to the question. Best,
@T.J.Crowder Damn! How can you write that fast!
@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... :-)
3

instead of doing this thing use below code

var arr = [];

arr.push(detailobj);

Comments

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);

Comments

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);

Comments

0

for example:

var idArray = [detailObj1, detailObj2];

Comments

0

Another one :

var myArray = new Array();
myArray.push(<your object>)

1 Comment

while it is good to know that the new Array constructor exists, i would never recommend using it over [].
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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.