/    Sign up×
Community /Pin to ProfileBookmark

How to write mthods for adding and getting values from dictionary ?

I am trying to create two methods to add or get values in a dictionary object based on specific key, how I can do that ? I know only how to create one like this:

[CODE]var dictionary = {};
dictionary.mykey = value;[/CODE]

and create methods like this:

[CODE]var dictionary = {
add: function (key, val) {
/* what I should write here ?? */
}

get: function (key, val) {
/* what I should write here ?? */
}
}[/CODE]

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@shakazorbaMar 30.2017 — https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set

[CODE]

<script>
var dictionary = {

add: function (key, val) {
this[key]=val;
},

get: function (key) {
return this[key];
}
}

dictionary.add('key1','value1');
alert(dictionary.get('key1'));
</script>

[/CODE]
Copy linkTweet thisAlerts:
@will123authorMar 30.2017 — thanks alot shakazorba
Copy linkTweet thisAlerts:
@rootMar 30.2017 — But that is not a dictionary, its a JSON string.

A Dictionary object is an ActiveXObject (Microsoft) thing that isn't supported in every browser.

If you want to create something like it then you should consider creating something that will use a JSON.stringify method to then store the data in a localStorage object if one can be made.
Copy linkTweet thisAlerts:
@shakazorbaMar 31.2017 — But that is not a dictionary, [COLOR="#FF0000"]its a JSON s[/COLOR]tring.

A Dictionary object is an ActiveXObject (Microsoft) thing that isn't supported in every browser.

If you want to create something like it then you should consider creating something that will use a JSON.stringify method to then store the data in a localStorage object if one can be made.[/QUOTE]


The object literal notation is not the same as the JavaScript Object Notation (JSON). Although they look similar.

this is an object literal Not a JSON string ...

[CODE]

var dictionary = {
add: function (key, val) {
/* what I should write here ?? */
}

get: function (key, val) {
/* what I should write here ?? */
}
}


[/CODE]


in this case dictionary is an arbitrary

name and is not in any way activeX
Copy linkTweet thisAlerts:
@rootApr 01.2017 — Object Literal / JSON String, not difference. In either case, they are not dictionary objects or dictionaries.
Copy linkTweet thisAlerts:
@will123authorApr 01.2017 — Object Literal / JSON String, not difference. In either case, they are not dictionary objects or dictionaries.[/QUOTE]

No this is wrong, I really dont find any other reasons why you insist in your wrong opinions other than you just spamming the forum !!! hope moderators do something about it
Copy linkTweet thisAlerts:
@iBeZiApr 01.2017 — Well [I]technically[/I] a JSON string is a string, it's just a data store, while a Javascript object is an object which can do a lot more than just store data.

Also "dictionary" is another name for a key:value data store.
Copy linkTweet thisAlerts:
@rootApr 01.2017 — A key:value is a data pair, why are people renaming things that have already got a name, is a factorial going to be renamed to multiply numbers together function?
Copy linkTweet thisAlerts:
@iBeZiApr 01.2017 — A key:value is a data pair, why are people renaming things that have already got a name, is a factorial going to be renamed to multiply numbers together function?[/QUOTE]

The key:value data structure is implemented differently in various languages, and is given a different name in each.

In Ruby they're called Hashes, Python they're dictionaries, in PHP they're associative arrays etc.

In JavaScript they're just objects, so they shouldn't really be called dictionaries in this case, but will123 might be more used to using Python.
Copy linkTweet thisAlerts:
@shakazorbaApr 01.2017 — A key:value is a data pair, why are people renaming things that have already got a name, is a factorial going to be renamed to multiply numbers together function?[/QUOTE]

https://en.wikipedia.org/wiki/Associative_array

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs
Copy linkTweet thisAlerts:
@shakazorbaApr 01.2017 — Object Literal / JSON String, not difference. In either case, they are not dictionary objects or dictionaries.[/QUOTE]

could you please provide an

example of 'dictionary objects or dictionaries'
Copy linkTweet thisAlerts:
@rootApr 01.2017 — MSIE
var y = new ActiveXObject("Scripting.Dictionary");
y.add ("a", "test");


You also have [B]localStorage[/B] thats also a name & value pair. Although not a dictionary, it can be used like one.
Copy linkTweet thisAlerts:
@shakazorbaApr 01.2017 — But that is not a dictionary, its a JSON string.

A Dictionary object is an ActiveXObject (Microsoft) thing that isn't supported in every browser.

If you want to create something like it then you should consider creating something that will use a JSON.stringify method to then store the data in a localStorage object if one can be made.[/QUOTE]


what would be the point of

using a JSON.stringify method to then store the data in a localStorage

why not like this ?


<script>

var dictionary = {

add: function (key, val) {
this[key]=val;
},

get: function (key) {
return this[key];
}

}

dictionary.add('a','test');

alert(dictionary.get('a'));

</script>

notice the similarity to this ...

var dictionary = new ActiveXObject("Scripting.Dictionary");

dictionary.add ("a", "test");
Copy linkTweet thisAlerts:
@rootApr 02.2017 — Persistence, and if you had read up about localStorage, that would have been blatantly obvious.
×

Success!

Help @will123 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...