Does JQuery support Dictionaries (key, value) collection?

JavascriptJqueryCollectionsDictionary

Javascript Problem Overview


Does JQuery support Dictionaries (key, value) collection ?

I would like to set the following data in a structure

[1, false]
[2, true]
[3, false]

with the ability to add, lookup, delete and update.

Any help!

Javascript Solutions


Solution 1 - Javascript

No, jQuery doesn't, but Javascript does.

Just use an object:

var dict = {
  "1" : false,
  "2" : true,
  "3" : false
};

// lookup:
var second = dict["2"];
// update:
dict["2"] = false;
// add:
dict["4"] = true;
// delete:
delete dict["2"];

Solution 2 - Javascript

jQuery, no. But JavaScript does. There are only two structures in JavaScript, arrays and objects.

Objects can be used as dictionary, where the properties are the "keys":

var dict = {
    1: true,
    2: true,
    3: false
};

Properties of objects can be either accessed with dot notation, obj.property (if the property name is a valid identifier, which a digit as used above is not) or with array access notation, obj['property'].

Solution 3 - Javascript

You don't need separate dictionary classes, since Javascript objects act as dictionaries. See this:

var userObject = {}; // equivalent to new Object()
userObject["lastLoginTime"] = new Date();
alert(userObject["lastLoginTime"]);

Full article here: http://msdn.microsoft.com/en-us/magazine/cc163419.aspx

Solution 4 - Javascript

With pure JavaScript,

var myDictionary = new Object();
myDictionary[1] = false;
myDictionary[2] = true;
myDictionary[3] = false;

function look(i) { return myDictionary[i];}
look(1); // will return false

Solution 5 - Javascript

Yes, you can use object to do this:

var myDict = { 1:false , 2:true , 3:false };

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHomamView Question on Stackoverflow
Solution 1 - JavascriptGuffaView Answer on Stackoverflow
Solution 2 - JavascriptFelix KlingView Answer on Stackoverflow
Solution 3 - JavascriptGeoView Answer on Stackoverflow
Solution 4 - JavascriptMithun SreedharanView Answer on Stackoverflow
Solution 5 - JavascriptBenoîtView Answer on Stackoverflow