I'm trying to read value from a json into a 2D array
var arr = [[],[]];
var i = 0;
var file = './test.json';
jsonfile.readFile(file, function(err, objs) {
let i = 0;
objs.forEach(function (obj) {
arr[i][0] = obj["outcount"];
arr[i][1] = obj["gateID"];
arr[i][2] = obj["timestamp"];
arr[i][3] = obj["eventCode"];
i = i + 1;
});
console.log(arr[2]);
})
For i = 0 and i = 1, it works fine but on i = 2 it gives the error
TypeError: Cannot set property '0' of undefined
arronly contains 2 elements, that you get witharr[0]andarr[1]but when you try to get a 3rd, non-existant element witharr[2]you getundefinedand then you try to access a property ofundefinedwhich gives you that TypeError