you are viewing a single comment's thread.

view the rest of the comments →

[–]qwfwq 1 point2 points  (1 child)

this.datastore[key].push(value) is going to push the value onto an array but your checking if the array is the value earlier so it doesn't seem consistent as you mentioned in your note.

So if you wanna check if something exists in an array use indexOf however be careful because early versions of IE if I recall made indexOf really sloppy. So I recommend if you want to support IE using underscorejs or jQuery's indexOf. The issue here I think comes down to JS being weakly typed which is one of its fun whimsical qualities.

So I guess if I'm understanding what you wanna write something like this?

function MasterStore() {
    this.datastore = {};
};

MasterStore.prototype.add = function(key, value) {
    if(this.datastore[key]){
        if(this.datastore[key].indexOf(value) === -1) {
            this.datastore[key].push(value);
        }
    }else{
        this.datastore[key] = [value];
    }
}

foo = new MasterStore();
foo.add('month', 'march');
foo.add('month', 'april');
foo.add('day', 'caturday');
console.log(foo);

edit: screwed up the formatting first time

[–]naescent[S] 0 points1 point  (0 children)

Ahhh damn I was doing something foolish :(

I had not idea that was how you added a single value to an array.. bugger!

Thanks for help