0

I am trying to figure out what is the best way to store and access data in javascript.

I want to select the data associated with a "identifier" who´s value is unknown before, or add a new data set with a new random "identifier".

do i do this with an array or object?

array storage

{
"data": [
    {
        "key1": "identfifier1",
        "key2": "value2",
        "key3": "value3"
    },
    {
        "key1": "identfifier2",
        "key2": "value2",
        "key3": "value3"
    }
]
}
  • arrays stay orderd in js. this is important for me becuase i want to display its data on screen.
  • more complicated selection of data set asscoictated with "identifier" (some kind of loop or containing helper method is needed, e.g. data.containing("identifier").key2)

object storage

{
"data": {
    "identfifier1": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    },
    "identfifier2": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    }
}
}
  • objects do not stay orderd in js, but that is important for me becuase its data to display on screen ( i would have to sort the whole object before each output of the data )
  • easy and fast selection of data asscoictated with "identifier" (just do identfifier1.key2 for expample, no loop or helper method nedded to search for data set containing "identifier")

any ideas, methods, best ways to do this? one way better than the other?

2
  • Where would this data be coming from? Server-side? Commented Dec 20, 2011 at 15:12
  • yes from the server, some user would manipulate some, maybe add or delete and send it back. it will all be in JSON. Commented Dec 20, 2011 at 15:15

2 Answers 2

1

Use JSON (your second approach). It's meant for that purpose and there are many native functions and additional libraries to support your work. It will be easy to use a database (e.g. CouchDB or MongoDB) with it, without having to convert objects.

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

Comments

1

If you do more reads than writes, then it's better to use the object and keep a separate array of the object keys that can be sorted when needed.

data = {
    identfifier1 : { key1: value1,
            key2: value2,
            key3: value3
          },
    identfifier2 : { key1: value1,
            key2: value2,
            key3: value3
          }
}

And when you add new key:

data.identifier3 = ...

Update the ordering:

var sorted = Object.keys( data ).sort();

To iterate in an ordered way:

for( var l = sorted.length, i = 0; i < l; ++i ) {
     alert( data[sorted[i]] );
}

Or you could just sort only when absolutely needed, this way even writes will be more performant.

You could make a wrapper "class" for all this that is fast for reading and sorts only when needed.

Comments

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.