简体   繁体   English

如何在Javascript中以数字为字符串的关联数组

[英]How to make associative array with number as string in Javascript

I have a code : 我有一个代码:

var index = 100;

var arr =[];

arr[index.toString()] = "Hello"

The result : index still known as integer not a string. 结果:索引仍然称为整数而不是字符串。 Anyone can explain what's wrong with my code? 任何人都可以解释我的代码有什么问题吗?

You have to declare associative arrays using {} , which creates a new object , because in JavaScript, arrays always use numbered indexes. 您必须使用{}声明关联数组 ,这会创建一个新object ,因为在JavaScript中,数组始终使用编号索引。

You need to declare an object: var arr={}; 您需要声明一个对象: var arr={};

  • arrays use numbered indexes . 数组使用编号索引
  • objects use named indexes . 对象使用命名索引

 var index = 100; var arr ={}; arr[index.toString()] = "Hello"; console.log(arr); 

How to make associative array with number as string in Javascript 如何在Javascript中以数字为字符串的关联数组

JavaScript doesn't have associative arrays in the sense that term is frequently used. 从没有经常使用术语的意义上讲,JavaScript没有关联数组。 It has objects, and as of ES2015 (aka "ES6"), it has Map s. 它具有对象,从ES2015(又称“ ES6”)开始,它具有Map

The result : index still known as integer not a string. 结果:索引仍然称为整数而不是字符串。 Anyone can explain what's wrong with my code? 任何人都可以解释我的代码有什么问题吗?

The index variable 's value is still a number, yes, because you haven't done anything to change it. index 变量的值仍然是一个数字,是的,因为您没有做任何更改。 But the index in the array is a string (and would be even if you didn't use .toString() ), because standard arrays aren't really arrays at all 1 , they're objects with special handling of a class of properties (ones whose names are strings that fit the spec's definition of an array index), a special length property, and that use Array.prototype as their prototype. 但是数组中的索引是一个字符串(即使您没有使用.toString() ),因为标准数组实际上并不是真正的数组 1 ,它们是经过特殊处理的一类属性的对象(其名称是符合数组索引的规范定义的字符串的名称),特殊的length属性,并使用Array.prototype作为其原型。

Here's proof that array indexes are strings: 这证明数组索引是字符串:

 var a = []; a[0] = "zero"; for (var name in a) { console.log("name == " + name + ", typeof name == " + typeof name); } 

That said, you don't want to use an array when you want a generic object or map. 就是说,当您想要通用对象或映射时,您不想使用数组。

Here's using a generic object for name/value mappings: 这里使用通用对象进行名称/值映射:

 var o = Object.create(null); var name = "answer"; o[name] = 42; console.log(o[name]); // 42 

The property names in objects are strings or (as of ES2015) Symbols. 对象中的属性名称是字符串或(从ES2015开始)符号。 I used Object.create(null) to create the object so it wouldn't have Object.prototype as its prototype, since that gives us properties ( toString , valueOf , etc.) that we don't want if we're using the object as a map. 我使用Object.create(null)创建对象,因此它不会以Object.prototype作为其原型,因为这会给我们提供我们不希望使用的属性( toStringvalueOf等)。对象作为地图。

Here's using a Map : 这是使用Map

 var m = new Map(); var name = "answer"; m.set(name, 42); console.log(m.get(name)); // 42 

The main advantages Map s have over objects are: Map与对象相比的主要优点是:

  • Their keys can be anything, not just strings or Symbols 它们的键可以是任何东西,而不仅仅是字符串或符号
  • They're iterable , so you can use for-of to loop through the mappings they contain 它们是可迭代的 ,因此您可以使用for-of来遍历它们包含的映射
  • Map s have a size property telling you how many entries they have Mapsize属性告诉您它们有多少个条目
  • Map s guarantee that iteration of their entries is performed in the order the entries were added to the map Map保证其条目的迭代按照条目添加到地图的顺序执行

With ES6, you could use a Map , which holds any type as key. 使用ES6,您可以使用Map ,该Map保留任何类型的键。

 var map = new Map; map.set(100, "Hello"); map.set('100', "Foo"); console.log(map.get(100)); // 'Hello' console.log(map.get('100')); // 'Foo' console.log([...map]); 

JavaScript does not support arrays with named indexes , in JavaScript, arrays always use numbered indexes. JavaScript不支持带有命名索引的数组 ,在JavaScript中,数组始终使用编号索引。

If you use a named index, JavaScript will redefine the array to a standard object. 如果您使用命名索引,JavaScript会将数组重新定义为标准对象。 After that, all array methods and properties will produce incorrect results. 之后,所有数组方法和属性将产生错误的结果。

As you can see in the following example: 如以下示例所示:

 var person = []; person["firstName"] = "John"; person["lastName"] = "Doe"; person["age"] = 46; var x = person.length; // person.length will return 0 console.log(x); var y = person[0]; // person[0] will return undefined console.log(y); 

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

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