作者:长白山天翼张薇_955 | 来源:互联网 | 2023-10-13 11:19
Whyusingstringsaskeysofarray,consoleisshowingthatarraywithoutthesedeclaredvaluesand
Why using strings as keys of array, console is showing that array without these declared values and while iterating by this values where keys are string aren't displayed? , although i can get value of them.
为什么使用字符串作为数组的键,console显示没有声明值的数组,并在不显示键的值的情况下迭代这些值?,虽然我可以得到它们的价值。
>> var arr = [ 0, 1, 2, 3 ];
undefined
>> arr["something"] = "aught";
"aught"
>> arr
[0, 1, 2, 3]
>> arr["something"]
"aught"
>> for( var i = arr.length; i--; console.log( arr[ i ] ) );
3
2
1
0
I understand that arrays are objects which has implemented some kind of 'enumerate' interface in Javascript's engine. Most interesting is that interpreter isn't throwing either warning or error so i spent a few of time of searching for where the data could be lost. I now, I was wrong and I used []
instead of {}
我知道数组是在Javascript引擎中实现了某种“枚举”接口的对象。最有趣的是,解释器不会抛出警告或错误,因此我花了一些时间搜索数据可能丢失的位置。我错了,我用了[]而不是{}
2 个解决方案