开发者

Array of Literal Arrays JavaScript

I have a JavaScript code like that:

var faqOpener = {
   defaults: {
      idSelector: "#id",
      id: "2"
   },
...
  if (opti开发者_JAVA技巧ons && !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.


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...


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]


Try this:

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

Can be used like this:

faqOpener.defaults[0].id


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.


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜