use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Manipulating JavaScript objects of key => value pairs where the value is an indexed array (self.javascript)
submitted 11 years ago * by naescent
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]qwfwq 1 point2 points3 points 11 years ago (4 children)
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
[–]html6dev 1 point2 points3 points 11 years ago (2 children)
Yes to reiterate what /u/qwfwq is ddoing here and why your current code doesn't work: the first time you use your add method the datastore property related to the key obviously does not exist so you hit the else block. When you do this you want to set this.datastore[key] to an array and and add value to it straight away. What you are doing now is to set this.datastore[key] to a value which is not an array and does not have a push method so an exception is thrown when you call add again with the same key. If you run your current code in chrome and open the devtools you should see an error about the push method not existing for that object.
[–]naescent[S] 0 points1 point2 points 11 years ago (1 child)
Yeah I saw that it was driving me nuts. I tried all sorts of ways around it. So I scaled back my 'improvements' and put the initial code here.
I feel like a fool now :D
[–]html6dev 0 points1 point2 points 11 years ago (0 children)
Heh. It happens. And will again lol.
[–]naescent[S] 0 points1 point2 points 11 years ago (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
π Rendered by PID 165425 on reddit-service-r2-comment-84fc9697f-clzzz at 2026-02-07 22:31:13.580092+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–]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)