2

The following will bring back the last item in this dictionary.

$mydict = {"1":"one", "5":"five", "5":"five"};
$elements = $mydict["5"];
console.log($elements);

http://jsfiddle.net/7pnb3xj8/

Why doesn't it bring back the last two items?

Are two of the same keys allowed?

Is there any reason to use jQuery variables when dictionaries are involved? From what I understand, jQuery doesn't know anything about dictionaries.

3
  • FYI, this has nothing to do with jQuery Commented Jul 12, 2015 at 17:00
  • Also, 'jQuery variables' are not different from any other variable. It is simply common practice to prefix a variable derived from a jQuery object or method call with a dollar symbol. Commented Jul 12, 2015 at 17:02
  • An object can contain only one value per key. Your literal is invalid in strict mode. Commented Jul 12, 2015 at 17:07

5 Answers 5

2

Dictionaries only allow one value for each unique key, so that dictionary you're defining is equivalent to:

var dict = { "1" : "one", "5" : "five" };

Also, i'm not sure what you mean with jquery variables, if you're talking about putting a '$' before your variable name, that's still a JS variable with a '$' in its name.

Sign up to request clarification or add additional context in comments.

Comments

1

In Javascript objects, the last key value will override any key with same name. thus,

$mydict = {"1":"one", "5":"five", "5":"six"};
$elements = $mydict["5"];

will print six, not five

Comments

1

It seems that, at runtime, duplicate enties from your object gets overridden with last entry. So your object $mydict = {"1":"one", "5":"five", "5":"five"}; becomes

$mydict = {"1":"one", "5":"five"};

Just try to log $mydict

Fiddle Demo- http://jsfiddle.net/7pnb3xj8/1/

Comments

0

First thing, whatever prefixed with $ is not jQuery thing. In JavaScript dictionaries does not allow duplicate keys. If you try to add duplicate key and value, it will override the existing value. JS Engine thinks that you are trying to change the value if it's duplicate else it will create new reference for it.

Comments

0

Technically those are objects, not dictionaries. Edit: As has been pointed out, objects, and dictionaries are technically equivalent and are just key value pairs. My original comment just reflected that the terminology used to refer to them in JavaScript is objects, so you'll have an easier time searching for information on JavaScript objects than JavaScript dictionaries.

I'm guessing you're coming from python? Objects can't have more than one value with the same key, so that object is invalid in JavaScript.

Edit: Also, those aren't jquery variables. There's no such thing as jQuery variables. I think you're misreading how people use jquery. They actually use the $ which is the jQuery selector, and invoke it with the () passing in an object to turn that object into a jQuery object. so it's not $myObject, it's $(myObject).

3 Comments

technically dictionary, map, hash table, object, associative array are all key-value based storage. it's just naming difference in a weakly typed language such as JavaScript
Oh I completely agree with you and that's correct. I just included that because the terminology in JavaScript for them is objects, and he'll have an easier time searching the internet for information on javascript objects than he will for javascript dictionaries. But I'll edit my original post to reflect that.
I see. Yes, having the correct term will help with the search. I hope he also sees the nature of all these storage structures and understands that the "keys", or "hash indexes", are results of a hash function. They are like buckets with labels. If you use the same label, the previous value will be replaced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.