I am getting the data in the following format
[Sl.No, Amount,开发者_C百科 Transaction Type, Account Number]
[01, $10000, Deposit, 473882829]
[02, $10202, Deposit, 348844844]
[02, $10202, Withdrawal, 348844844]
What is the best way to store this data in Javascript for faster retrieval
var data = ["02", "$10202", "Withdrawal", 348844844]
//empty object
var list = {};
//Constructing the index by concatenating the last two elements of the data.
//Probably this will give the primary key to the data.
var index = data[2] + data[3].toString();
//Store the data using the index
list[index] = data;
You can retrieve the data using the index constructed above.
Determine how the data needs to be accessed. I am guessing it needs to be accessed linearly, so as to not accidentally overdraw (withdraw before deposit), etc -- in this case an Array is generally a suitable data structure. A simple for-loop should be able to find most simple "queries".
Define how the object is represented in Javascript -- is each "transaction item" an array of
[x, amount, type, account]
or is it an object with the signature{x: ..., amount: ..., type: ..., account: ...}
(I often opt for the latter as it adds some self-documentation). It could also be an object created withnew TransactionItem
(or whatnot) that includes methods.Determine how to convert the raw data into the chosen object representation. For a well-defined format a simple regular expression or 'split' may work.
Use data. Remember that even a shoddy
O(n^2)
algorithm is generally "fast enough" for smalln
. Best to get it working first.
Happy coding.
精彩评论