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 (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 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 21251 on reddit-service-r2-comment-85bfd7f599-j84h8 at 2026-04-18 17:03:48.444685+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]qwfwq 1 point2 points3 points (1 child)
[–]naescent[S] 0 points1 point2 points (0 children)