【发布时间】:2022-11-04 21:04:21
【问题描述】:
我有一个数组。据我所知,数组键是 javascript 中的整数。
const array1 = ['a', 'b', 'c'];
当我得到并记录键时,我得到一个整数数组。
console.log([...array1.keys()]);
// Outputs=> [0, 1, 2]
但是在 for...in 循环中,键是字符串。但是为什么以及有没有办法为整数键键入强制转换?
for (const key in array1) {
console.log("Type of key "+key+" is "+ typeof key);
}
/* outputs:
Type of key 0 is string
Type of key 1 is string
Type of key 2 is string
*/
【问题讨论】: