如何在 Javascript 文字符号中创建关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37515959/
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
how to create an associative array in Javascript literal notation
提问by Wild Widow
I understand that there are no associative arrays
in javascript
, only objects
.
我知道没有associative arrays
in javascript
,只有objects
。
However I can create an array
with string keys using bracket notation
like this:
但是我可以像这样array
使用字符串键创建一个bracket notation
:
var myArray = [];
myArray['a'] = 200;
myArray['b'] = 300;
console.log(myArray); //prints [a: 200, b: 300]
so, I want to do the exact same thing without using bracket notation
所以,我想在不使用的情况下做完全相同的事情 bracket notation
var myNewArray = [a: 200, b: 300]; //getting error - Unexpected token:
this does not work either
这也不起作用
var myNewArray = ['a': 200, 'b': 300]; //same error, why can I not create?
回答by Lux
JavaScript has no associative arrays, just objects. Even JavaScript arrays are basically just objects, just with the special thing that the property names are numbers (0,1,...).
JavaScript 没有关联数组,只有对象。甚至 JavaScript 数组也基本上只是对象,只是属性名称是数字(0,1,...)这一特殊之处。
So look at your code first:
所以先看看你的代码:
var myArray = []; // creating a new array object
myArray['a'] = 200; // setting the attribute a to 200
myArray['b'] = 300; // setting the attribute b to 300
It's important to understand that myArray['a'] = 200;
is identicalto myArray.a = 200;
!
要明白,这是非常重要的myArray['a'] = 200;
是相同的到myArray.a = 200;
!
So to start with what you want: You can't create a javascript array and pass no number attributes to it in one statement.
所以从你想要的开始:你不能创建一个 javascript 数组并且在一个语句中不传递任何数字属性。
But this is probably not what you need! Probably you just need a JS object, what is basically the same as an associative array, dictionary, or map in other languages: It maps strings to values. And that can be done easily:
但这可能不是您需要的!可能你只需要一个 JS 对象,它与其他语言中的关联数组、字典或映射基本相同:它将字符串映射到值。这很容易做到:
var myObj = {a: 200, b: 300};
But it's important to understand that this differs slightly from what you did. myObj instanceof Array
will return false
, because myObj
is not an ancestor from Array
in the prototype chain.
但重要的是要了解这与您所做的略有不同。myObj instanceof Array
将返回false
,因为myObj
它不是Array
原型链中的祖先。
回答by Santo Boldi?ar
You can using Map:
您可以使用地图:
var arr = new Map([
['key1', 'User'],
['key2', 'Guest'],
['key3', 'Admin'],
]);
var res = arr.get('key2');
console.log(res); // The value is 'Guest'
回答by httpNick
You want to use an object in this case
在这种情况下你想使用一个对象
var myObject = {'a' : 200, 'b' : 300 };
This answer links to a more in-depth explanation: How to do associative array/hashing in JavaScript(http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/)
这个答案链接到更深入的解释:How to do associative array/hashing in JavaScript( http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/)
回答by galchen
Well, you are creating an array, which is in fact an object.
好吧,您正在创建一个数组,它实际上是一个对象。
var arr = [];
arr.map;
// function(..)
arr['map'];
// function(..)
arr['a'] = 5;
console.log(arr instanceof Object); // true
You can add fields and functions to arr
. It does not "insert" them to the array though (like arr.push(...)
)
您可以将字段和函数添加到arr
. 它不会将它们“插入”到数组中(例如arr.push(...)
)
You can refer to an object fields with []
syntax
您可以使用[]
语法引用对象字段
回答by developius
Arrays and Objects are different things.
数组和对象是不同的东西。
For an object, you can do things like:
对于对象,您可以执行以下操作:
var obj = { "property1": "value1" };
obj.property2 = "value2";
For arrays, you have to do this:
对于数组,你必须这样做:
var arr = [];
arr[0] = "value";
回答by milen
You can do what you wanted to do this way:
你可以用这种方式做你想做的事:
myNewArray = new Array ({'a' : 200, 'b' : 300})
myNewArray = 新数组 ({'a' : 200, 'b' : 300})