Javascript多维数组错误
Javascript multidimensional array error
我尝试在 angular 控制器中用数据填充二维数组,但出现错误 Cannot set 属性 '0' of undefined at controllers.js:74 。
代码如下:
var firstmatchday = 1;
var empty = [[],[]];
var counter1 = 0;
var counter2 = 0;
$scope.fixtures = data.fixtures;
for(i=0; i<data.fixtures.length; i++)
{
if(data.fixtures[i].matchday == firstmatchday)
{
empty[counter1][counter2++] = data.fixtures[i];
}
else
{
counter1++;
counter2 = 0;
firstmatchday++;
empty[counter1][counter2++] = data.fixtures[i]; // line 74
//console.log(data.fixtures[i]);
}
}
我的代码有什么问题?
谢谢托米斯拉夫
你不能在 javascript 中使用二维数组,它不存在,如果它必须是二维的,它必须是对象
empty = {
property1 : {
property : "foo"
}
}
empty[属性1][属性] === "foo" 会 return 是的,你不能像 [=] 那样做 [[],[]] 39=] 因为 javascript 中的数组要么是简单的 [1,2,3] 要么是 [{},{},{}] 实际上不是
empty = [
0 : [
property : value
]
]
但是有
empty = [
{
property : value
}
]
您可以将其作为空[0]访问。属性
如果你有数组
empty = ["foo","bar"];
空[1]将return"bar"
如果你有对象
empty = [
{
property : "bar"
}
]
var _prop = "property";
empty[0][_prop] will return also "bar"
因为当您不知道实际的 属性 或索引名称时(它是动态访问的,您不能使用点符号
empty.0._prop 因为它会寻找
empty = {
0 : {
_prop : "bar"
}
}
所以只有javascript的插值方法才能访问对象/数组中的动态properties/indexes
var firstmatchday = 1;
var empty = {};
var counter1 = 0;
var counter2 = 0;
$scope.fixtures = data.fixtures;
for(i=0; i<data.fixtures.length; i++)
{
if(data.fixtures[i].matchday == firstmatchday)
{
if(emtpy[counter1] === undefined)
{
empty[counter1] = {};
}
if(empty[counter1][counter2++] === undefined)
{
empty[counter1][counter2++] = {};
}
empty[counter1][counter2++] = data.fixtures[i];
}
else
{
counter1++;
counter2 = 0;
firstmatchday++;
empty[counter1][counter2++] = data.fixtures[i]; // line 74
//console.log(data.fixtures[i]);
}
}
在第一个循环中:
- 第一个比赛日 === 1
- 计数器 1 === 0
- counter2 === 0
- 我=== 0
假设data.fixtures[0].matchday !== 1
- 第一个比赛日 === 2
- counter1 === 1
- counter2 === 0
- empty[1][0] === [] // 可以赋值 data.fixtures[0]
- counter2 === 1
在下一个循环中:
- 我 === 1
假设data.fixtures[1].matchday !== 2
- 第一个比赛日 === 3
- counter1 === 2
- counter2 === 0
- empty[2][0] === undefined[0] // ERROR 无法赋值 data.fixtures[1]
- counter2 === 1
这会导致错误,因为 JS 会首先解释 empty[2]
并看到它是 undefined
。所以 empty[2][0]
等同于 undefined[0]
会导致错误。这就是为什么添加 empty[counter1] = []
可以解决问题,因为现在示例中 empty[2]
等于 []
,因此可以访问 empty[2][0]
。
我尝试在 angular 控制器中用数据填充二维数组,但出现错误 Cannot set 属性 '0' of undefined at controllers.js:74 。
代码如下:
var firstmatchday = 1;
var empty = [[],[]];
var counter1 = 0;
var counter2 = 0;
$scope.fixtures = data.fixtures;
for(i=0; i<data.fixtures.length; i++)
{
if(data.fixtures[i].matchday == firstmatchday)
{
empty[counter1][counter2++] = data.fixtures[i];
}
else
{
counter1++;
counter2 = 0;
firstmatchday++;
empty[counter1][counter2++] = data.fixtures[i]; // line 74
//console.log(data.fixtures[i]);
}
}
我的代码有什么问题?
谢谢托米斯拉夫
你不能在 javascript 中使用二维数组,它不存在,如果它必须是二维的,它必须是对象
empty = {
property1 : {
property : "foo"
}
}
empty[属性1][属性] === "foo" 会 return 是的,你不能像 [=] 那样做 [[],[]] 39=] 因为 javascript 中的数组要么是简单的 [1,2,3] 要么是 [{},{},{}] 实际上不是
empty = [
0 : [
property : value
]
]
但是有
empty = [
{
property : value
}
]
您可以将其作为空[0]访问。属性 如果你有数组
empty = ["foo","bar"];
空[1]将return"bar"
如果你有对象
empty = [
{
property : "bar"
}
]
var _prop = "property";
empty[0][_prop] will return also "bar"
因为当您不知道实际的 属性 或索引名称时(它是动态访问的,您不能使用点符号
empty.0._prop 因为它会寻找
empty = {
0 : {
_prop : "bar"
}
}
所以只有javascript的插值方法才能访问对象/数组中的动态properties/indexes
var firstmatchday = 1;
var empty = {};
var counter1 = 0;
var counter2 = 0;
$scope.fixtures = data.fixtures;
for(i=0; i<data.fixtures.length; i++)
{
if(data.fixtures[i].matchday == firstmatchday)
{
if(emtpy[counter1] === undefined)
{
empty[counter1] = {};
}
if(empty[counter1][counter2++] === undefined)
{
empty[counter1][counter2++] = {};
}
empty[counter1][counter2++] = data.fixtures[i];
}
else
{
counter1++;
counter2 = 0;
firstmatchday++;
empty[counter1][counter2++] = data.fixtures[i]; // line 74
//console.log(data.fixtures[i]);
}
}
在第一个循环中:
- 第一个比赛日 === 1
- 计数器 1 === 0
- counter2 === 0
- 我=== 0
假设data.fixtures[0].matchday !== 1
- 第一个比赛日 === 2
- counter1 === 1
- counter2 === 0
- empty[1][0] === [] // 可以赋值 data.fixtures[0]
- counter2 === 1
在下一个循环中: - 我 === 1
假设data.fixtures[1].matchday !== 2
- 第一个比赛日 === 3
- counter1 === 2
- counter2 === 0
- empty[2][0] === undefined[0] // ERROR 无法赋值 data.fixtures[1]
- counter2 === 1
这会导致错误,因为 JS 会首先解释 empty[2]
并看到它是 undefined
。所以 empty[2][0]
等同于 undefined[0]
会导致错误。这就是为什么添加 empty[counter1] = []
可以解决问题,因为现在示例中 empty[2]
等于 []
,因此可以访问 empty[2][0]
。