1

I know JavaScript doesn't support associate array. We will create an object with a key as a string.

Here is https://jsfiddle.net/maheshwaghmare/sgw07L8L/6/

E.g.  "hometitle":[
           ["fontsize","12px"],
           ["lineheight","16px"],
       ],

But, I need to create it like:

E.g.  ".home-title":[
           ["font-size","12px"],
           ["line-height","16px"],
       ],

So, In this situation how can I store and retrieve values without storing repetitive values in same parent.

E.g. (.home-title) doesn't contain another (font-size)

NOTE: Parents(.home-title) may be - (.home-title .headings) - (#header .title) etc

I know we could not store this kind of keys in object.

Any solution..! Thanks in Advance...!

1 Answer 1

4

To easily access the properties (and avoid duplicate keys) you would use objects rather than an arrays of arrays.

There is nothing that stops you from using that kind of string as keys in an object.

Example:

var obj = {
  ".home-title": {
    "font-size": "12px",
    "line-height": "16px"
  },
  ".home-title .headings": {
    "font-size": "16px",
    "line-height": "20px"
  },
  "#header .title": {
    "font-size": "12px",
    "font-weight": "bold"
  }
};
Sign up to request clarification or add additional context in comments.

6 Comments

+1 - good, concise answer. Only improvement I can think of is removing reduntant quotation marks around key names - key always has to be (and is, even if by coercion) a string in JS.
@bardzusny: All those quotation marks are necessary because of the non-identifier characters in the property names.
@Lye Fish - that is right, sorry for missing that. Disregard my comment.
@Guffa Thanks, I will try your solution to fix my issue.
@maheshwaghmare. That's very much overcomplicated. You just need two lines in the saveDate function: jsfiddle.net/sgw07L8L/7 Here are some comments on the code: jsfiddle.net/sgw07L8L/10
|

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.