简体   繁体   English

JavaScript 数组如何具有非数字键?

[英]How can JavaScript arrays have non-numeric keys?

What I have learned is an array is a type of object.我学到的是数组是一种对象。 Objects are a collection of properties with key/value pairs.对象是具有键/值对的属性集合。 I always thought arrays are a collection of items that are numerically indexed starting at 0. Just recently, I was able to add a non-numeric key to an array.我一直认为数组是从 0 开始数字索引的项目的集合。就在最近,我能够向数组添加一个非数字键。

let arr = ['cribriform plate','mastoid','hyoid'];
arr.eyes = 'brown';
arr.skin = 'white';

That resulted in这导致

['cribriform plate','mastoid','hyoid',eyes : 'brown', skin : 'white'];

The for...in loop of arr yielded: arr 的 for...in 循环产生了:

for(let i in arr){
    console.log(i);
    //0,1,2,3,eyes,skin
}

The for...of loop yielded: for...of 循环产生:

for(let i of arr){
     console.log(i);
     //0,1,2,3
}

I was able to iterate over all the keys of an array using the for...in loop.我能够使用 for...in 循环遍历数组的所有键。 However, when I used the for...of loop, I was only able to iterate over the numerically indexed keys.但是,当我使用 for...of 循环时,我只能迭代数字索引键。 Why is that?这是为什么?

And, what is the most accurate definition of an array?而且,数组最准确的定义是什么?

With a for..of loop, the object's Symbol.iterator property is called.使用for..of循环,调用对象的Symbol.iterator属性。 In the case of an array, this is equivalent to the array's .values() method, which contains the values for each index in the array .对于数组,这等效于数组的.values()方法,该方法包含数组中每个索引的值 Non-numeric properties are not included - arrays generally don't have arbitrary non-numeric properties, and code that does assign arbitrary non-numeric properties to an array is likely in need of refactoring.非数字属性不包括-阵列通常不具有任意的非数字属性,这确实分配任意的非数字属性到一个数组代码很可能需要重构的。

A for..in loop iterates over all enumerable properties on an object, including those inherited from the prototype. for..in循环遍历对象上的所有可枚举属性,包括从原型继承的属性。 Thus, for..of on an array will exclude non-numeric properties on an array that a for..in loop will include.因此, for..of上的for..of将排除for..in循环将包含的数组上的非数字属性。

Arrays, being objects, can have arbitrary properties assigned to them, for the most part, just like properties can be assigned to ordinary functions - it's just not a very good idea.数组作为对象,可以为它们分配任意属性,在大多数情况下,就像可以将属性分配给普通函数一样 - 这不是一个很好的主意。

Arrays are a type of objects in javascript.数组是 JavaScript 中的一种对象。 When you do something like arr.skin = 'white';当你做类似arr.skin = 'white';事情时, you're basically setting a variable to the array's object property collection. ,您基本上是为数组的对象属性集合设置一个变量。 That's why you can access it with in for...of , which iterates over the enumerable properties of the object.这就是为什么您可以使用 in for...of访问它,它遍历对象的可枚举属性。

However, since this new property is not part of the array's list of elements, it cannot be accessed through for...in但是,由于这个新属性不是数组元素列表的一部分,因此无法通过for...in访问它

Taken from MDN web docs for Arrays :摘自Arrays 的 MDN 网络文档

Setting or accessing via non-integers using bracket notation (or dot notation) will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array's object property collection.使用括号表示法(或点表示法)通过非整数设置或访问不会从数组列表本身设置或检索元素,但会设置或访问与该数组的对象属性集合关联的变量。 The array's object properties and list of array elements are separate, and the array's traversal and mutation operations cannot be applied to these named properties.数组的对象属性和数组元素列表是分开的,数组的遍历和变异操作不能应用于这些命名属性。

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

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