Is a multidimensional array what I'm looking for?

Seems like the main thing for most of you, but I'm stuck with this issue:

So, I have 3 categories of fields:

-Category 1 = [field1, field2, field3.. etc]
-Category 2 = [field1, field2, field3.. etc]
-Category 3 = [field1, field2, field3.. etc]

      

And besides, I have other items that have all of these categories, but not all of these fields:

 Element 1 = Category 1[field3, field2], Category2[Field4], Category3[field1, field5, field2]

      

How can I organize this data in javascript (I am using JQuery if it can help)?

+3


source to share


2 answers


Many programming languages ​​support named indexed arrays.

Arrays with named indexes are called associative arrays (or hashes).

JavaScript does NOT support named-index arrays.

In JavaScript, arrays always use numbered indices.



You can use objects instead:

{
    "Element1": {
        "Category1": [
            "field3",
            "field2"
        ],
        "Category2": [
            "field4"
        ],
        "Category3": [
            "field1",
            "field5",
            "field2"
        ]
    }
}

      

You can take a look at MDN - it's a complete newbie to JavaScript and its data types.

Eloquent Javascript can also help build the javascript framework.

+2


source


It depends on what you are trying to do with the data, but one way is:



var category1 = ['field1', 'field2', ... ];

var element1 = { 
  category1: ['field3', 'field2'],
  ...
}

      

0


source







All Articles