0

I want to iterate through a database and store the information as an array or object. I don't really care which as long as I can store and retrieve based on a key. I need guidence on the syntax to use to create and store data in javascript

// create storageEntity as array or objects

storageEntity = {};
storageEntity = [];

// on each retreival from data base store data in "storageEntity" // conceptually it should look like this:

storageEntity[recordIFromFile][recordType][dataType] = value

where:

  • recordIFromFile - is an numeric value from file
  • recordType - is a string from file
  • dataType - is a string from file
  • value - is a numeric value from the file

2 Answers 2

1

You want to use an object/dictionary, i.e.

var storageEntity = {};

If your ID is 99999, you'd need 99998 empty entries in an array, I guess this isn't what you want. Now if you want to store a piece of data:

var record = storageEntity[recordIFromFile];
if (record == undefined) {
  record = {};
  storageEntity[recordIFromFile] = record;
}

var byType = record[recordType];
if (byType == undefined) {
  byType = {};
  record[recordType] = byType;
}
byType[dataType] = value;
Sign up to request clarification or add additional context in comments.

Comments

0

Why can't you just request a JSON from your server and work over that instead?

3 Comments

actually data comes from server, gets placed on page, then I use jquery to selectively lift values to update back to server.
That's not good, request JSON, make the table with jquery, not the other way around.
Your advice is interesting, but has not much to do with the question. Data is added to page via two different asynchronous data feeds and user input. Be careful trying to throw the same solution at every problem.

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.