JavaScript的array.forEach自定义按键不灵 [英] Javascript array.forEach with custom keys not working
问题描述
我已动态生成阵列
var userData = new Array();
var userHash = "HashCode123";
// now I push object to that array:
userData[userHash].username = "Ferdo";
userData[userHash].ban = 0;
控制台返回这个值:
Console returning this values:
userData
[] //why this is not array of object(s)?
userData[hash]
Object {username: "Ferdo", ban: 0}
我需要运行 userData.forEach()
,但我不能因为数组是空的?谢谢你的帮助。
I need run userData.forEach()
but I can't because array is empty? Thank you for help.
推荐答案
的forEach
只遍历元素的数组。唯一属性的属性名称具有一个数值,是0和2之间 32 -2被认为是该阵列的元素
forEach
only iterates over elements of an array. Only properties whose property name has a numeric value that is between 0 and 232-2 are considered to be elements of the array.
散列code123
不符合这一标准。如果你想随心所欲的钥匙的名字,你可以使用一个对象,并遍历,与其(我如何枚举一个JavaScript对象的属性?)。
"HashCode123"
doesn't fulfill this criteria. If you want arbitrary "key" names, you can use an object and iterate over that instead (How do I enumerate the properties of a JavaScript object?).
这篇关于JavaScript的array.forEach自定义按键不灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!