17

Is there exist any kind of c# dictionary in JavaScript. I've got an app in angularjs that requests data from an MVC Web Api and once it gets, it makes some changes to it. So the data is an array of objects, which is stored in the MVC Web Api as a Dictionary of objects, but I convert it to list before passing it throug network.

If I convert the Dictionary directly to JSon I get something like:

array = [ {Id:"1", {Id:"1", Name:"Kevin Shields"}}, 
          {Id:"2", {Id:"2", Name:"Natasha Romanoff"}}
        ];

Well the objects are a little more complex, but you've got now an idea. The problem is that this format is even harder to operate with (I've got alphabetical keys or ids). So is there any equivalent to a dictionary? It's quite simple to do thing like:

Object o = dictionary["1"];

So that's it, thank in advance.

m.dorian
  • 489
  • 1
  • 6
  • 24
  • http://stackoverflow.com/questions/368280/javascript-hashmap-equivalent – Habib Nov 24 '14 at 15:18
  • If you can assume keys are unique, just make an object. If you can't you'll need to construct two arrays – Paul S. Nov 24 '14 at 15:20
  • If your lookup keys are unique strings (or easily converted to unique strings), then the Javascript object will work just fine for storing and looking up those keys and data associated with the keys. – jfriend00 Nov 24 '14 at 15:21
  • I find it sad that .Net does not have a equivalent of `{key1: "val1}` translates to Dictionary(of String, Object); a data type I depend on heavily. This would be helpful for c# and vb. – Timothy C. Quinn Jul 05 '22 at 16:06

4 Answers4

20

You have two options really, although both essentially do the same thing, it may be worth reading a bit more here, which talks about associative arrays (dictionaries), if you wish to tailor the solution:

var dictionary = new Array(); 
dictionary['key'] = 'value'

Alternatively:

var dict = []; 

dict.push({
    key:   'key',
    value: 'value'
});

Update

Since ES2015 you can use Map():

const dict = new Map();
dict.set('{propertyName}', {propertyValue});
ediblecode
  • 11,701
  • 19
  • 68
  • 116
  • 1
    Link is 404. First example issue: `dictionary.length === 0` Second example issue: `dict['key'] === undefined`. So neither of these really work as even a basic C# dictionary. – Erik Philips Dec 13 '18 at 14:21
  • This is the right answer, for the time being, if one sends a dictionary from javascript to the controller. Neither NewtonSoft nor System.Text.Json can be trusted. NewtonSoft's serialized json does not survive the net framework's deserialization, and System.Text.Json raises System.NotSupportedException because it does not do Dictionary for example. – scrat.squirrel May 14 '21 at 16:47
13

I know this question is a bit older, but in ES2015 there is a new data structure called map that is much more similar to a dictionary that you would use in C#. So now you don't have to fake one as an object, or as an array.

The MDN covers it pretty well. ES2015 Map

JGTaylor
  • 1,072
  • 8
  • 13
5

Yes, it's called an object. Object have keys and values just like C# dictonaries. Keys are always strings.

In your case the object would look like this:

{
    "1": {
        "Id": 1, 
        "Name":" Kevin Shields"
    }, 
    "2": {
        "Id": 2, 
        "Name": "Natasha Romanoff"
    }
}

The default ASP.net serializer produces ugly JSON. A better alternative would be Json.NET.

dusky
  • 1,133
  • 7
  • 12
0

My Example:

var dict = new Array();
// add a key named id with value 111
dict.id = 111;
//change value of id
dict.id = "blablabla";
//another way
// add a key named name with value "myName"
dict["name"] = "myName";
//and delete
delete dict.id;
delete dict["name"]

//another way
dict = {
  id:    111,
  "name": "myName"
};

//And also another way create associate array
var myMap = { key: [ value1, value2 ] };
isxaker
  • 8,446
  • 12
  • 60
  • 87