且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Javascript中创建关联数组

更新时间:2022-01-23 04:28:15

JavaScript没有关联数组".

JavaScript doesn't have "associative arrays".

它具有对象,您可以在命名属性中存储值.

It has objects, which you can store values on in named properties.

它具有数组,这些数组通过整数属性存储一组有序的值.由于它们是对象的一种,因此您也可以在它们上存储命名值(但是您不能使用文字语法来创建它们,只能在创建数组之后添加它们),但是这并不是一种好的做法,它们会被几乎所有对数组有特殊情况处理的事物都将其忽略.

It has arrays, which store an ordered set of values via integer properties. Since they are a type of object, you can also store named values on them (but you can't create them using the literal syntax, only add them after the array is created) but this isn't considered good practise and they will be ignored by just about anything that has special case handling for arrays.

从ES6开始,它还具​​有地图与对象相似,因为您可以给它们命名的值,但也保留按此顺序排列的数组.没有用于创建地图的文字语法.

As of ES6 it also has Maps which are similar to objects in that you can give them named values, but also to arrays in that order is preserved. There is no literal syntax for creating a Map.