wuno wuno - 7 months ago 48
Javascript Question

Working With Multi Functional Array In Javascript

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

Answer

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 semi-colon after the 176th index. https://jsfiddle.net/6h7bedtL/1/