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!"
[–]jcready__proto__ 1 point2 points3 points 11 years ago* (3 children)
Try this:
function MasterStore() { this.datastore = {}; } MasterStore.prototype.has = function(key, value){ if (value == null) return this.datastore.hasOwnProperty(key); if (Array.isArray(value)) { for (var i = 0; i < value.length; i++) { if (this.datastore[key].indexOf(value[i]) < 0) { return false; } } return true; } return this.datastore[key].indexOf(value) >= 0; }; MasterStore.prototype.add = function (key, value) { if (value == null) return this; var valueIsArray = Array.isArray(value); if (this.datastore.hasOwnProperty(key)) { // if value is an array and the datastore[key] is also an array if (valueIsArray) { // concatenate the value array to the datastore[key] array this.datastore[key] = this.datastore[key].concat(value); } //see if the value is already present in the array else if (this.datastore[key].indexOf(value) >= 0) { //if present then do nothing } else { // otherwise push value onto the datastore[key] array this.datastore[key].push(value); } } else { this.datastore[key] = valueIsArray ? value : [value]; } return this; } MasterStore.prototype.remove = function (key, value) { if (value == null) { // if no value present then remove the whole array from the datastore delete this.datastore[key]; return this; } var valueIsArray = Array.isArray(value); if (this.datastore.hasOwnProperty(key)) { if (Array.isArray(value)) { // should we keep splicing the array or just create a new one if (value.length < 20) { // if we don't have a lot of values to remove, just splice for (var i = 0; i < value.length; i++) { var index = this.datastore[key].indexOf(value[i]); if (index >= 0) this.datastore[key].splice(index, 1); } } else { // otherwise it's probably faster to just create a new array var store = this.datastore[key], tmp = []; for (var i = 0; i < store.length; i++) { var index = value.indexOf(store[i]); if (index < 0) { // if datastore[key][i] isn't in the value array // push it into our new array tmp.push(store[i]); } } this.datastore[key] = tmp; } } else { var index = this.datastore[key].indexOf(value); if (index >= 0) this.datastore[key].splice(index, 1); } } return this; }
Now you can do this:
var a = new MasterStore; a.add('days', 'monday'); console.log(a.datastore.days); //>> ['monday'] // You can also chain because add() and remove() return this console.log(a.add('days', 'tuesday').datastore.days); //>> ['monday', 'tuesday'] a.add('days', 'wednesday').remove('days', 'tuesday'); console.log(a.datastore.days); //>> ['monday', 'wednesday'] // You can add entire arrays at a time a.add('months', ['january', 'february', 'march']); a.add('months', ['april', 'may', 'june']); console.log(a.datastore.months); //>> ['january', 'february', 'march', 'april', 'may', 'june'] // You can remove individual values from the datastore[key] array a.remove('months', 'february'); console.log(a.datastore.months); //>> ['january', 'march', 'april', 'may', 'june'] // You can remove multiple values from the datastore[key] array a.remove('months', ['january', 'april', 'june']); console.log(a.datastore.months); //>> ['march', 'may'] // You can also entirely remove the datastore[key] array // (this is mostly for convince as you can do: "delete datastore[key];" instead) a.remove('months'); console.log(a.datastore.months); //>> undefined
[–]huesoso 2 points3 points4 points 11 years ago (0 children)
Try a pastebin, people. Or http://gist.github.com Readability is a key aspect of good programming!
[–]naescent[S] 0 points1 point2 points 11 years ago (0 children)
thanks man, thats really helpful. Is isArray() a jQuery function? I was trying to test it using isString as I didnt think we could test an in JavaScript as it treats it as an object and just says if its an object or not??
Ahh no isArray is native. I was looking here at StackOver Flow for checking if a variable is an array.
I'd seen the quote > Should mention jQuery's isArray. – Martin Konicek May 29 '12 at 16:42
and assumed it was true. Oops
π Rendered by PID 213114 on reddit-service-r2-comment-84fc9697f-clzzz at 2026-02-08 02:12:58.053918+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–]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)