简体   繁体   English

JavaScript 中的 C# 字典等效项

[英]C# Dictionary equivalent in JavaScript

Is there exist any kind of c# dictionary in JavaScript. JavaScript 中是否存在任何类型的 c# 字典。 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.我在 angularjs 中有一个应用程序,它从 MVC Web Api 请求数据,一旦获得,它就会对其进行一些更改。 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.所以数据是一个对象数组,它作为对象字典存储在 MVC Web Api 中,但我在通过网络传递它之前将其转换为列表。

If I convert the Dictionary directly to JSon I get something like:如果我将字典直接转换为 JSon,我会得到类似的信息:

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).问题是这种格式更难操作(我有按字母顺序排列的键或 ID)。 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.就是这样,提前致谢。

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() :从 ES2015 开始,您可以使用Map()

const dict = new Map();
dict.set('{propertyName}', {propertyValue});

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#.我知道这个问题有点旧,但在 ES2015 中有一个名为 map 的新数据结构,它更类似于您将在 C# 中使用的字典。 So now you don't have to fake one as an object, or as an array.所以现在你不必将一个伪装成一个对象或一个数组。

The MDN covers it pretty well. MDN 很好地涵盖了它。 ES2015 Map ES2015地图

Yes, it's called an object .是的,它被称为object Object have keys and values just like C# dictonaries.对象具有键和值,就像 C# 字典一样。 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.默认的 ASP.net 序列化程序生成丑陋的 JSON。 A better alternative would be Json.NET .更好的选择是Json.NET

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 ] };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM