Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JQuery support Dictionaries (key, value) collection?

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!

like image 325
Homam Avatar asked Mar 23 '11 07:03

Homam


People also ask

Can you use dictionaries in JavaScript?

Are there dictionaries in JavaScript? No, as of now JavaScript does not include a native “Dictionary” data type. However, Objects in JavaScript are quite flexible and can be used to create key-value pairs. These objects are quite similar to dictionaries and work alike.

What are dictionaries in JavaScript?

A dictionary is a general-purpose data structure for storing a group of objects. A dictionary has a set of keys and each key has a single associated value. When presented with a key, the dictionary will return the associated value.


2 Answers

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"]; 
like image 150
Guffa Avatar answered Sep 17 '22 13:09

Guffa


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'].

like image 38
Felix Kling Avatar answered Sep 17 '22 13:09

Felix Kling