I understand that there are no associative arrays
in javascript
, only objects
.
我知道javascript中没有关联数组,只有对象。
However I can create an array
with string keys using bracket notation
like this:
但是,我可以使用括号表示法创建一个包含字符串键的数组,如下所示:
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
所以,我想在不使用括号表示的情况下做同样的事情
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?
5 个解决方案
#1
7
Javascript has no associative arrays, just objects. Even JS arrays are basically just objects, just with the special thing that the property names are numbers (0,1,...).
Javascript没有关联数组,只有对象。甚至JS数组基本上也只是对象,只是特殊的东西,属性名称是数字(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 300
Its important to understand that myArray['a'] = 200;
is identical to 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 instance of Array
will return false
, because myObj is not an ancestor from Array in the prototype chain.
但重要的是要明白,这与你所做的略有不同。 myObj Array的实例将返回false,因为myObj不是原型链中Array的祖先。
#2
2
You can using Map:
你可以使用Map:
var arr = new Map([
['key1', 'User'],
['key2', 'Guest'],
['key3', 'Admin'],
]);
var res = arr.get('key2');
console.log(res); // The value is 'Guest'
#3
1
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/)
这个答案链接到一个更深入的解释:如何在JavaScript中进行关联数组/散列(http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/)
#4
1
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";
#5
1
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
您可以使用[]语法引用对象字段
#1
7
Javascript has no associative arrays, just objects. Even JS arrays are basically just objects, just with the special thing that the property names are numbers (0,1,...).
Javascript没有关联数组,只有对象。甚至JS数组基本上也只是对象,只是特殊的东西,属性名称是数字(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 300
Its important to understand that myArray['a'] = 200;
is identical to 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 instance of Array
will return false
, because myObj is not an ancestor from Array in the prototype chain.
但重要的是要明白,这与你所做的略有不同。 myObj Array的实例将返回false,因为myObj不是原型链中Array的祖先。
#2
2
You can using Map:
你可以使用Map:
var arr = new Map([
['key1', 'User'],
['key2', 'Guest'],
['key3', 'Admin'],
]);
var res = arr.get('key2');
console.log(res); // The value is 'Guest'
#3
1
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/)
这个答案链接到一个更深入的解释:如何在JavaScript中进行关联数组/散列(http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/)
#4
1
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";
#5
1
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
您可以使用[]语法引用对象字段