在Javascript中使用多功能阵列

时间:2016-11-25 04:01:17

标签: javascript arrays

jsfiddle包含崩溃但未运行的所有数据。

jsfiddle运行较少的数据,但如果你

console.log(`row ${g}, col ${i}`);
console.log(obj[pastWinners]);

它显示未定义。

我在Javascript中有一个多维数组,它为我运行一些计算。此array转换为object literal,然后将一些数据操作更改为json object并最终转换为字符串。该计划效果很好!

我必须在多维数组中再添加310行。当我这样做时,我得到了这个错误,

  

index.html:331 Uncaught TypeError:无法读取属性“0”   未定义

原始数组看起来像这样,

var pastWinners = [
    [2, 9, 19, 23, 38, 40],
    [17, 25, 31, 35, 38, 43],
    [8, 10, 17, 30, 33, 43],
    [10, 17, 26, 28, 36, 43],
    [14, 20, 25, 28, 34, 41],
    [8, 13, 21, 23, 25, 43],
    [10, 11, 18, 24, 27, 30],
    [21, 22, 23, 26, 33, 39],
    [6, 21, 23, 29, 36, 40],
    [10, 12, 16, 21, 25, 42],
    [1, 11, 20, 27, 34, 37]
];

请注意,单位数字在十位上没有0。但新数据看起来像这样,

[05, 07, 17, 18, 33, 35], 

它在十位有一个0。我之所以提到这一点,是因为除了它之外还有310多行到阵列是唯一的区别。我已经检查了50次数据并且没有任何问题。

这样的小差异是否会导致像这样的错误?它说错误是在我的程序中的331行抛出,

   var obj = {};

for (var g = 0; g < pastWinners.length; g++) {
    for (var i = 0, j = pastWinners.length; i < j; i++) {
        if (obj[pastWinners[g][i]] == undefined) {
            console.log(`row ${g}, col ${i}`);
            console.log(obj[pastWinners]);
        }
        if (obj[pastWinners[g][i]]) { //this is line 331
            obj[pastWinners[g][i]]++;
        } else {
            obj[pastWinners[g][i]] = 1;
        }
    }
}

var picks = [];
for (var numbs in obj) {
    picks.push([numbs, obj[numbs]])
    picks.sort(
        function(a, b) {
            return a[1] - b[1]
        }
    )
}

picks.reverse();
var topPicks = []

for (var winners = 0; winners < 6; winners++) {
    topPicks.push(picks[winners][0]);
}

var weekPicks = topPicks.toString();
console.log(weekPicks);
alert(weekPicks);

当我添加此数据时,是否有人发现这种情况会发生?如果它是数组大小的问题,我该如何处理这个问题,因为我还要添加几千行数据。

row 0, col 0
index.html:333 undefined
index.html:332 row 0, col 1
index.html:333 undefined
index.html:332 row 0, col 2
index.html:333 undefined
index.html:332 row 0, col 3
index.html:333 undefined
index.html:332 row 0, col 4
index.html:333 undefined
index.html:332 row 0, col 5
index.html:333 undefined
index.html:332 row 0, col 6
index.html:333 undefined

5 个答案:

答案 0 :(得分:1)

尝试在未定义时在控制台中打印数组,并在日志中检查数组大小。

for (var g = 0; g < pastWinners.length; g++) {
    for (var i = 0, j = pastWinners.length; i < j; i++) {
        if (obj[pastWinners[g][i]] == undefined){
         console.log(`row ${g}, col ${i}`);
         console.log(obj[pastWinners]);
        }
        if (obj[pastWinners[g][i]]) { //this is line 331
            obj[pastWinners[g][i]]++;
        } else {
            obj[pastWinners[g][i]] = 1;
        }
    }
}

答案 1 :(得分:1)

通过在错误发生时添加console.log(g)您正在使用的索引,这实际上是您的数据的问题:

[2, 7, 8, 9, 27, 38],
[9, 12, 17, 24, 26, 41]
[18, 20, 21, 31, 40, 44],

您在第176个索引后缺少逗号。 https://jsfiddle.net/6h7bedtL/1/

答案 2 :(得分:0)

我会采用不同的方式:

一个。改变第二个循环...

湾定义obj

var pastWinners = [
    [2, 9, 19, 23, 38, 40],
    [17, 25, 31, 35, 38, 43],
    [8, 10, 17, 30, 33, 43],
    [10, 17, 26, 28, 36, 43],
    [14, 20, 25, 28, 34, 41],
    [8, 13, 21, 23, 25, 43],
    [10, 11, 18, 24, 27, 30],
    [21, 22, 23, 26, 33, 39],
    [6, 21, 23, 29, 36, 40],
    [10, 12, 16, 21, 25, 42],
    [1, 11, 20, 27, 34, 37], 
];
var obj=[];
for (var g = 0; g < pastWinners.length; g++) {
    for (var i = 0; i < pastWinners[g].length; i++) {
        if (obj[pastWinners[g][i]]) { //this is line 331
            obj[pastWinners[g][i]]++;
        } else {
            obj[pastWinners[g][i]] = 1;
        }
    }
}

console.log(obj)

答案 3 :(得分:0)

此外,您应该在循环之前检查是否存在pastWinners[g],或者尝试使用

访问子数组

if (pastWinners[g] != null) {

原因是如果由于某种原因未定义pastWinners[g],那么您的脚本将尝试执行此操作:

undefined[i]或从undefined获取[i]的值 - 它不能因为undefined不是数组 - 这会破坏你的脚本

&#13;
&#13;
var pastWinners = [
  [2, 9, 19, 23, 38, 40],
  [17, 25, 31, 35, 38, 43],
  [8, 10, 17, 30, 33, 43],
  [10, 17, 26, 28, 36, 43],
  [14, 20, 25, 28, 34, 41],
  [8, 13, 21, 23, 25, 43],
  [10, 11, 18, 24, 27, 30],
  [21, 22, 23, 26, 33, 39],
  [6, 21, 23, 29, 36, 40],
  [10, 12, 16, 21, 25, 42],
  [1, 11, 20, 27, 34, 37],
];
for (var g = 0; g < pastWinners.length; g++) {

  if (pastWinners[g] != null) {  // check for valid item

    for (var i = 0, j = pastWinners[g].length; i < j; i++) {

      if (pastWinners[g][i] != null) {
        pastWinners[g][i]++;
      } else {
        pastWinners[g][i] = 1;
      }
    }
  }
}

console.log(pastWinners);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

可能

 for (var i = 0, j = pastWinners.length; i < j; i++) 

应该是

for (var i = 0, j = pastWinners[g].length; i < j; i++)

pastWinners中的数组项的长度不是pastwinners:D