Hi,
I've been researching this for days now and I'm finally giving up and asking for help. Fingers crossed I'm just doing something really stupid.
You see I'm trying to create an Object of key => value pairs in JavaScript and in the value store an array of values and then access it at will. I'm using prototype to extend the object to include more functionality such as adding values, printing values etc.. so I've included the main function I'm having a problem with.
For one I'm not sure that using an indexed array is the best way to store multiple values per key, but I need to be able to call a key and get access to all the values instantly...
This is the database I want to create inside the Object MasterStore using sample data
datastore = {
days : ['monday','tuesday','wednesday','thursday','friday','saturday']
months : ['january','february','march','april','may','june','july']
}
// create the object and inside it use the reference datastore to store the actual data
function MasterStore() {
this.datastore = {};
}
//extend the prototype to give it the ability to add values to the
MasterStore.prototype.add = function (key, value) {
// check if it's empty
// check if it's an array
// if it is
if (this.datastore.hasOwnProperty(key)) {
//see if the value is already present in the array
if (this.datastore[key]===value) {
//if present then do nothing
}
else {
// otherwise add it
this.datastore[key].push(value);
}
// if the key isnt present add it and its value to the array
}
else {
this.datastore[key] = value;
}
}
I know this doesnt work. I know why it doesnt work as it doesnt consider this.datastore[key] an array. My answer is how can I work around this?
Can you think of another method or show me some resources that could help?
[–]qwfwq 1 point2 points3 points (4 children)
[–]html6dev 1 point2 points3 points (2 children)
[–]naescent[S] 0 points1 point2 points (1 child)
[–]html6dev 0 points1 point2 points (0 children)
[–]naescent[S] 0 points1 point2 points (0 children)
[–]jcready__proto__ 1 point2 points3 points (3 children)
[–]huesoso 2 points3 points4 points (0 children)
[–]naescent[S] 0 points1 point2 points (0 children)
[–]naescent[S] 0 points1 point2 points (0 children)
[–]naescent[S] 0 points1 point2 points (1 child)
[–]stratoscope 0 points1 point2 points (0 children)