简体   繁体   中英

What's the easiest way to store and load data Javascript?

I have a Hashmap for storing Loudness values. The code is in Javascript HTML5.

var Loudness = {};
Loudness['f125'] = {right: [], left: []};
Loudness['f125']['right'].push([0,10,30, 40]);
Loudness['f125']['left'].push([5,15,35,45]);
Loudness['f250'] = {right: [], left:[] };
Loudness['f500'] = {right: [], left: []};
Loudness['f1000'] = {right: [], left: []};

I also have a Hashmap for storing Gain values (basically the same as up, also w/ "right" and "left" (right and left side of the audiospeakers) properties.
The difference is the name of the HM and the values of the array. I have a lot of frequencies (keys, for instance f250, f500, …) still to insert to each Hashmap. Is there a more efficient way to declare each frequency key and its properties?
Does it make sense to use a Hashmap inside a Hashmap?
Also, if the Gain HM is put inside the Loudness HM, how would the new Loudness HM disposition be like? Do the keys(frequencies) remain the same? Is the GainHashmap now a new property? I haven't found how to implement it. What would the syntax be like?
What's the easiest way to insert and easily change data ("loudness" and "gain" for "right" and "left", for each frequency).
Is there a better option in your opinion? WebStorageAPI, Hashmap inside Hashmap?
Please consider that I'm not such an expert in programming. Any help is greatly Appreciated.

var Loudness = {};
    var freqs = ['f125', 'f250', 'f500', 'f1000', 'f1250'];
    var values = [0, 10, 30, 40, 5, 15, 35, 45];
    for(var i in freqs){
        Loudness[freqs[i]] = {left : [], right : []};
        Loudness[freqs[i]].Gain = {left : [], right : []};
        for(var j in values){
            var v = values[j];
            if(v === 0 || v % 2 === 0){
                Loudness[freqs[i]]['right'].push(v);
                Loudness[freqs[i]].Gain['right'].push(v); 
            } else {
                Loudness[freqs[i]]['left'].push(v);
                Loudness[freqs[i]].Gain['left'].push(v); 
        }
    }
}

Try this one. In order to access your gains:

Loudness['f125'].Gain.left[0]

Basically if you need more frequencies, just add them to freqs arrays.

So, the idea of Gain as property inside the Loudness HM is perfect.
The problem is that this Loudness HM is not allowed to be accessed (wherever and however it was declared).
1) I tried to make it acessible even through privileged methods returning it.
2) I even declared it inside a constructor (using this.Loudness = {}; instead of var Loudness = {};, but am receiving errors like "Uncaught TypeError: Cannot read property '250' of undefined", when trying to access this value. Piece of the public method which uses it:

AudiometrySample.prototype.loudnessToGain = function (frequency, loudness, activeChannel) {
...
console.log("this.Loudness[250] is"+ this.Loudness);
if (this.activeChannel === "right") {
  for (var j = 0; j < this.Loudness[this.frequency].right.length; j++) {
    if (this.Loudness[this.frequency].right[j] === this.loudness) {
      this.wantedGain = this.Loudness[this.frequency].Gain.right[j];
      this.currentPairIndex = j;
    }
  }
}

This console log gives "undefined" back. How to make it accessible?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM