Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Dictionary equivalent in JavaScript

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.

like image 820
m.dorian Avatar asked Nov 24 '14 15:11

m.dorian


1 Answers

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});
like image 70
ediblecode Avatar answered Sep 21 '22 06:09

ediblecode