0

I have a JavaScript code like that:

var faqOpener = {
   defaults: {
      idSelector: "#id",
      id: "2"
   },
...
  if (options && !jQuery.isEmptyObject(options))
     $.extend(this.defaults, options);
...

How can I convert that variable as an array of literal arrays something like:

var faqOpener = {
   defaults[]: {
      idSelector: "#id",
      id: "2"
   },
...

EDIT: I will use that defaults variable from another javascript code. As shown in the example it has only one element at array of literal arrays however I should define it correctly and will able to pass variable length(for example array of literal arrays that has 3 array or 1 or more it depends) array of literal arrays.

5 Answers 5

4
var faqOpener = {
   defaults: [{
      idSelector: "#id1",
      id: "1"
   }, 
   {
      idSelector: "#id2",
      id: "2"
   }],
...

or straight

var defaults = [{
   idSelector: "#id1",
   id: "1"
}, 
{
   idSelector: "#id2",
   id: "2"
}];

The question is rather unclear...

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

Comments

3
var faqOpener = {
   defaults: [
       { idSelector: "#id", id: "2" },
       { idSelector: "#id1", id: "21" }
   ]
};

Then you can access the objects in defaults:

faqOpner.defaults[0] 
faqOpner.defaults[1]

2 Comments

Maybe the items in "defaults" are gigantic objects
the "presentation" won't fit on one line
1

Try this:

var faqOpener = {
   defaults: [{
      idSelector: "#id",
      id: "2"
   }],
...

Can be used like this:

faqOpener.defaults[0].id

Comments

1

Do you want faqOpener.defaults to be an array of objects { idSelector, id }? In JS, an array literal is delimited by square brackets, so it'd look like this:

var faqOpener = {
    defaults: [ 
       {
              idSelector: "#id",
              id: "2"
           },
           {
              idSelector: "#id3",
              id: "3"
           },
        ]
    };

That's not an array of arrays, exactly; it's an array of associative arrays. An "array" in JavaScript is a particular object type with some special characteristics. Any old JS object is an assocative array.

1 Comment

Any variable or property in JS can hold a reference to an array of associative arrays -- or more tersely, an array of objects; any JS object is an associative array. Sorry, I should have used that term and saved you some typing! What the answers have given you is how to declare a literal array of objects/AAs. If you've already got such an array from somewhere else, and you just want to assign it to a member, that's the easiest thing in the world: faqOpener.defaults = arrayfromwherever;
0

This is commonly known as JSON (JavaScript Object Notation):

http://www.json.org/example.html

1 Comment

Actually, strictly speaking, JSON is a separate markup language that is derived from and that mimicks Javascript's syntax. Though you're right, the syntax will be identical here.

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.