简体   繁体   English

数组的Javascript对象未定义

[英]Javascript object of arrays undefined

I have the following function: 我有以下功能:

function get_stoplight_color(count, position) {
    console.log(count + ', ' + position);

    var colors = {
        'QB' : ['#8b3a25', '#ceca63', '#68c24c', '#68c24c', '#68c24c'],
        'RB' : ['#8b3a25', '#ceca63', '#ceca63', '#ceca63', '#68c24c'],
        'WR' : ['#8b3a25', '#ceca63', '#ceca63', '#ceca63', '#ceca63'],
        'TE' : ['#8b3a25', '#ceca63', '#68c24c', '#68c24c', '#68c24c'],
        'DST' : ['#8b3a25', '#68c24c', '#68c24c', '#68c24c', '#68c24c'],
        'K' : ['#8b3a25', '#68c24c', '#68c24c', '#68c24c', '#68c24c']
    };

    console.log(colors);
    if (count <= 4) return colors.position[count];
    else return '#68c24c';
}

As you can see, I've logged a couple vars out to the console, and they appear to be just fine there. 如您所见,我已经在控制台中登录了两个var,它们在那里似乎还不错。 However, at runtime I'm getting the following error: 但是,在运行时出现以下错误:

Uncaught TypeError: Cannot read property '4' of undefined 未捕获的TypeError:无法读取未定义的属性“ 4”

(That's when count = 4.) (那时count =4。)

Again, the object colors clearly exists, because console.log(colors) outputs: 同样,对象colors显然存在,因为console.log(colors)输出:

Object {QB: Array[5], RB: Array[5], WR: Array[5], TE: Array[5], DST: Array[5]…}

So I would expect colors.position[count] to return #68c24c when position = 'QB' and count = 4. 因此,当position ='QB'且count = 4时,我希望colors.position[count]返回#68c24c

Why is the object still returning undefined in this case? 为什么在这种情况下对象仍返回undefined Thank you, and I'll provide more details if necessary (I've provided everything I've thought of up front). 谢谢,如有必要,我将提供更多详细信息(我已经提供了我想到的所有内容)。

If position is a variable then you should say colors[position][count] . 如果position是变量,那么您应该说colors[position][count] Otherwise JS will think position is a property of colors and not resolve its value. 否则,JS会认为positioncolors的属性,而不是解析其值。

The expression colors.position[count] uses a position property on colors . 表达式colors.position[count]colors上使用position属性。 I think you meant colors[position][count] , which will look up the property named by the variable position (eg, QB ) on colors instead, and then look up the property named by count (eg, 4 ) on the result. 我认为您的意思是colors[position][count] ,它将代替在colors上查找由变量position (例如QB )命名的属性,然后在结果上查找由count (例如4 )命名的属性。

If there's any chance position may not be defined, I would change that bit at the end slightly. 如果可能的position可能不确定,我将在末尾稍微更改一下。 Instead of: 代替:

if (count <= 4) return colors.position[count];
else return '#68c24c';

perhaps: 也许:

return (colors[position] && colors[position][count]) || '#68c24c';

...which takes advantage of JavaScript's curiously-powerful || ...利用JavaScript 强大的|| and && operators . &&运算符


Re your comment below: 在下面重新发表您的评论:

Wow, OK, that worked, but I'm not sure how? 哇,好,那行得通,但是我不确定怎么办? I didn't even try that since I view [] operands as indexes of an array, while colors is an object (not an array). 我什至没有尝试过,因为我将[]操作数视为数组的索引,而color是一个对象(而不是数组)。

In JavaScript (unlike many languages), you can access an object property in two ways: 在JavaScript中(不同于许多语言),您可以通过两种方式访问​​对象属性:

  1. Using dot notation and a literal property name, eg obj.foo (provided the property name is a valid identifier name ), or 使用点表示法和文字属性名称,例如obj.foo (假设属性名称是有效的标识符名称 ),或者

  2. Using bracketed notation and a string property name, eg obj["foo"] (and in this case, the property name can be just about anything, it doesn't have to be a valid identifier name). 使用带括号的表示法和字符串属性名称,例如obj["foo"] (在这种情况下,属性名称几乎可以是任何东西,它不必是有效的标识符名称)。

In the second case, the string can be from any expression, including a variable lookup. 在第二种情况下,字符串可以来自任何表达式,包括变量查找。 So all of these refer to obj.foo : 因此,所有这些都引用obj.foo

obj.foo
obj["foo"]
obj["f" + "o" + "o"]
var x = "o";
obj["f" + x + x]

...and in fact, this is is the same notation you use when dealing with arrays, because in JavaScript, arrays aren't really arrays , they're just objects. ...事实上,这您处理数组时使用的表示法相同 ,因为在JavaScript中, 数组并不是真正的数组 ,它们只是对象。

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

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