简体   繁体   English

如何在 JavaScript 文字符号中创建关联数组

[英]How to create an associative array in JavaScript literal notation

I understand that there are no associative arrays in JavaScript, only objects .我知道 JavaScript 中没有关联数组,只有objects

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]; // I am getting error - Unexpected token:

This does not work either:这也不起作用:

var myNewArray = ['a': 200, 'b': 300]; // Same error. Why can I not create?

JavaScript has no associative arrays, just objects. JavaScript 没有关联数组,只有对象。 Even JavaScript arrays are basically just objects, just with the special thing that the property names are numbers (0,1,...).甚至 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;重要的是要了解myArray['a'] = 200; is identical to 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 JavaScript object, what is basically the same as an associative array, dictionary, or map in other languages: It maps strings to values.可能你只需要一个 JavaScript 对象,它与其他语言中的关联数组、字典或映射基本相同:它将字符串映射到值。 And that can be done easily:这很容易做到:

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的祖先。

You can use Map:您可以使用地图:

var arr = new Map([
   ['key1', 'User'],
   ['key2', 'Guest'],
   ['key3', 'Admin'],
]);

var res = arr.get('key2');
console.log(res); // The value is 'Guest'

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这个答案链接到更深入的解释: How to do associative array/hashing in JavaScript

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 .您可以将字段和函数添加到arr It does not "insert" them into the array though (like arr.push(...) ).它不会将它们“插入”到数组中(如arr.push(...) )。

You can refer to an object fields with the [] syntax.您可以使用[]语法引用对象字段。

I understand that there are no associative arrays in JavaScript, only objects .我知道 JavaScript 中没有关联数组,只有objects

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]; // I am getting error - Unexpected token:

This does not work either:这也不起作用:

var myNewArray = ['a': 200, 'b': 300]; // Same error. Why can I not create?

I achieved this by using objects.我通过使用对象实现了这一点。 Your create an object, and loop through using for in loop.您创建一个对象,并通过使用 for in 循环进行循环。 each x will be the index and holder[x] will be the value.每个 x 将是索引, holder[x] 将是值。 an example is below.下面是一个例子。

var test = {'hello':'world','hello2':'world2'}
for(let x in holder)
{
  let inxed = x;
  let value = holder[x]
  console.log('index ' + x + ' has value of ' +    value)
}

Associate array is an array indexed with name similar to an object instead of numbers like in regular array .关联数组是一个索引名称类似于对象的数组,而不是常规数组中的数字 You can create an associative array in the following way:您可以通过以下方式创建关联数组:

 var arr = new Array(); // OR var arr = []; arr['name'] = 'david' arr['age'] = 23; console.log(arr['name']);

你可以用这种方式做你想做的事:

myNewArray = new Array ({'a' : 200, 'b' : 300})

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

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