JavaScript 中的 C# 字典等效项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27108082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Dictionary equivalent in JavaScript
提问by m.dorian
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.
JavaScript 中是否存在任何类型的 c# 字典。我在 angularjs 中有一个应用程序,它从 MVC Web Api 请求数据,一旦获得,它就会对其进行一些更改。所以数据是一个对象数组,它作为对象字典存储在 MVC Web Api 中,但我在通过网络传递它之前将它转换为列表。
If I convert the Dictionary directly to JSon I get something like:
如果我将 Dictionary 直接转换为 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). So is there any equivalent to a dictionary? It's quite simple to do thing like:
好吧,这些对象有点复杂,但您现在已经有了一个想法。问题是这种格式更难操作(我有按字母顺序排列的键或 ID)。那么有没有相当于字典的东西?做这样的事情很简单:
Object o = dictionary["1"];
So that's it, thank in advance.
就是这样,提前致谢。
回答by ediblecode
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'
});
回答by JGTaylor
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.
我知道这个问题有点旧,但在 ES2015 中有一个名为 map 的新数据结构,它更类似于您将在 C# 中使用的字典。所以现在你不必把一个伪装成一个对象,或者一个数组。
The MDN covers it pretty well. ES2015 Map
MDN 很好地涵盖了它。ES2015地图
回答by dusky
Yes, it's called an object. Object have keys and values just like C# dictonaries. Keys are always strings.
是的,它被称为object。对象具有键和值,就像 C# 字典一样。键总是字符串。
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.
默认的 ASP.net 序列化程序生成丑陋的 JSON。更好的选择是Json.NET。
回答by isxaker
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 ] };