Search code examples
javascriptarrayslocal-storageuserscripts

Local storage + 2-dimensional arrays


I'm trying to store information about icons into local storage. I'm going to make a settings form which displays each icon with information for each icon.

The information I'm trying to store:

"Icon Name", "Icon Link", "Description"

I need to be able to add different icons every time I change the settings.

Here's an example:

var icons = [["cat","/cat.png","Cat picture"],["dog","/OqSC.png","Dog Picture"]];

NOTE: I have no control of where the icons are going to be stored (Probably on IMGUR), each array will be determined by the user. I'm going to need to be able to append arrays into local storage and be able to retrieve the data and place them into 2 dimensional arrays.

I don't need someone to code the whole system for me, I just need to be pointed into the right direction on HOW I could do this. A few code examples would be appreciated as well.

Also: If this is the wrong path to go, tell me what I need to use. (i.e if 2 dimensional arrays shouldn't be used and I should use prototypes tell me).

Note #2: I'm making a User-script which is why I need local storage.

Thank you for taking your time to read my post and help me out.


Solution

  • I think you are trying too hard to create a DB like structure out of a JSON structure, and are going about this the wrong way.

    With JSON you should have a 1D array of objects, these objects would then have 3 keys (the ones you mention in your question).

    So you'd have a structure that looks like this:

    [{
      'name': '',
      'icon': '',
      'description': ''
    }, {
      'name': '',
      'icon': '',
      'description': ''
    },
    ...]