1

I'm working on a way to store temporary data (it will be stored in a database through ajax after a few minutes of being in use). The method I've chosen is to create a huge array, with arrays inside and then use functions to isolate the required data.

Code available here: https://jsfiddle.net/44bjgykq/5/

var property_data = [[5,[[8,[1,3,3,7],[[4,2],[7,3],[1,4]]],[6,[5,5,5,5],[[2,3],[6,4],[3,2]]]]],[5,[[5,[1,1,1,1],[[1,1],[1,1],[1,1]]],[7,[1,1,1,1],[[1,1],[1,1],[1,1]]]]]];

I feel like this might not be the most efficient/best practice way to store data, but I might be wrong. What would be a better alternative if there are any?

2 Answers 2

1

If you're looking for the absolute smallest footprint possibly then this is fine, albeit very hard to read and understand.

A more common approach would be to store it in a more friendly format, like a javascript object. For example:

var property = {
    propertyId: 5,
    rooms: [{
        roomId: 8
    },{
        roomId: 9
    }]
}

JS Fiddle: https://jsfiddle.net/ncocsr4y/

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

1 Comment

This looks like a much better solution than mine! Taking into account the amount of code needed for each function, they would probably workout with similar footprints. Thanks for the suggestion, I don't know how I've gone this long without using objects.
0

I would definitively use some kind of JSON nested dictionaries instead of arrays within arrays. It would take a little more space to transfer and to save in the database, but it will be more explicit for yourself and for other developers who will develop your project.

2 Comments

I'm trying to keep it all inside js before I save, and I have a pretty limited knowledge of JSON. I'll definitely look into this though, thanks for the suggestion!
You can use the structure like in Matt Whetton's answer and then json_string = JSON.stringify(property) to convert it to JSON string and property = JSON.parse(json_string) to convert it back to JavaScript object.

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.