0

jsfiddle with all the data which crashes and doesn't run.

jsfiddle with less data which runs but if you

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

It shows it is undefined.

I have a multi dimensional array in Javascript which runs some calculations for me. This array gets converted to a object literal and then after some data manipulation is changed to a json object and eventually a string. The program works great!

I have to add 310 more lines to the multi dimensional array. When I do I get this error,

index.html:331 Uncaught TypeError: Cannot read property '0' of undefined

The original array looks like this,

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]
];

Notice the single digit numbers do not have a 0 in the tens place. But the new data looks like this,

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

It does have a 0 in the tens place. I mention this because from aside from it being 310 more lines to the array that is the only difference. I have checked the data 50 times and there is nothing wrong with it.

Would a small difference like that make a difference to cause an error like this? It says the error is getting throws at line 331 in my program,

   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);

Does anyone see any reason this would happen when I add this data in? If it is an issue of the size of the array how can I handle this because I have a few thousand more lines of data to add in.

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
16
  • does 'obj' is defined? does it have a value of '0' ? Commented Nov 25, 2016 at 4:06
  • why have you taken it as "obj[pastWinners[g][i]]" shouldn't it be just pastWinners[g][i] Commented Nov 25, 2016 at 4:10
  • there is a trailing comma , at the end of your data Commented Nov 25, 2016 at 4:16
  • No there is not. I am not showing all the data cause it would be too much to add ot here. Commented Nov 25, 2016 at 4:29
  • There is a fundamental flaw in your code - even with the small amount of data, you get obj.undefined having a value of 55 - which is 11x11 - 11x6 - as the way you are processing the data, you'll always be getting lots of undefined ... with the 321 lines of data you actually have, you'll end up with obj.undefined: 3465 Commented Nov 25, 2016 at 4:41

5 Answers 5

1

By adding a console.log(g) of what index you were up to when the error happens, this is actually an issue with your data:

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

You are missing a comma after the 176th index. https://jsfiddle.net/6h7bedtL/1/

Sign up to request clarification or add additional context in comments.

Comments

1

Try to print the array in the console when you get undefined and check your array size in the log.

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;
        }
    }
}

3 Comments

Hey man I added the output from the console.log you provided to my question. Yes it does show undefined but I am not really sure what to gather from that.
I updated my question with the full program so you can see what I am doing.
Well, it looks like all the data inside is undefined. I don't know the algorithm you are using but try to debug from developer panel section source (if using chrome). If every time obj[pastWinners[g][i]] is undefined that means it always goes to the else condition. And if your error is thrown in the If statement it means at some point you are trying to index undefined. That means g is going to a line outside pastWinners and pastWinners[g] is undefined and you try to index it with pastWinners[g][i]. Just debug it and see what is happening
0

I would do it differently:

a. change the second loop...

b. define 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)

1 Comment

When I try this way I get index.html:329 Uncaught TypeError: Cannot read property 'length' of undefined(…) But I do not understand why is the length of pastWinners undefined?
0

Also you should check for the existence of pastWinners[g] before you loop through it or try to access a child array using

if (pastWinners[g] != null) {

The reason for this is that if pastWinners[g] for some reason is not defined, then your script will attempt to do this:

undefined[i] or get the value of [i] from undefined -- which it cant because undefined is not an array -- which breaks your script

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);

Comments

0

Possibly

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

should be

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

The array items in pastWinners do not have the length of pastwinners :D

1 Comment

Thanks but no this does not work either. Man I just don't even know where to go. This is really annoying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.