-1

I am trying to create a multidimensional array in javascript.

The following code generates the following error: 0x800a138f - JavaScript runtime error: Unable to set property '0' of undefined or null reference

var data = [["Timestamp", "kWh Produced", "kWh Used"]];

data[1][0] = "test";

I assume that I need to somehow "pre-allocate" the array so it knows that it is multi-dimensional.

I will be filling this array in one row at a time in a for loop if that makes anything simpler.

How do I do this?

Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
Pfredd
  • 417
  • 6
  • 15
  • 5
    You can only access `data[0]` which equals `["Timestamp", "kWh Produced", "kWh Used"]`. To add an item to `data` use `data.push()`. – Striped Feb 26 '18 at 15:57
  • 1
    `data[1]` doesn't exist on your example, only `data[0]` – t3__rry Feb 26 '18 at 15:58
  • `data[1]` is not defined, try `data[0][0]` – phuzi Feb 26 '18 at 15:59
  • Multidimensional arrays aren't really a thing in JS (JS *arrays* aren't actually arrays at all). You can have arrays within arrays though, but all of the 'sub-arrays' are completely separate from each other, so yes, you have to create every sub-array individually. – Lennholm Feb 26 '18 at 16:01
  • Adding `data[1] = []` before `data[1][0] = "test"` should sort it too. – Andrew Bone Feb 26 '18 at 16:06

5 Answers5

1

This code will fix your problem data[1] = [] it creates a new array at this position.

var data = [["Timestamp", "kWh Produced", "kWh Used"]];
data[1] = [];
data[1].push("test")
console.log(JSON.stringify(data)); // [["Timestamp","kWh Produced","kWh Used"],["test"]]
аlex
  • 5,426
  • 1
  • 29
  • 38
1

As your log indicates, data[1] is undefined.

There are a number of ways to define the values of data[0–99] as empty arrays. In es6 for instance, Array(100).fill([]). See https://stackoverflow.com/a/41246860/1664393.

user
  • 173
  • 6
0

The problem is that "data" is an array with a single element (an array of three strings), so data[1] is undefined.

var data = [["Timestamp", "kWh Produced", "kWh Used"]];
// data[1] // => undefined

You simply need to append (or assign) new values to the data array to make use of them.

data.push([]); // or data[1] = []
data[1]; // => []
data[1][0] = "test";
data[1]; // => ["test"]
data[1].push("foo");
data[1]; // => ["test", "foo"]
maerics
  • 151,642
  • 46
  • 269
  • 291
0
  • In JS as others programming languages, the array's indexes are zero-based.
  • You're trying to get a value from index 1, however that index is equals than the current array length = 1.

  0 <--- Position 
  |
  v                
[ ["Timestamp", "kWh Produced", "kWh Used"] ]
                                            ^
                                            |
                                            +---- Length = 1

So, data[1] is invalid because is beyond the array's boundaries.

You need to get the first position: data[0][0] and replace it with the new value.

var data = [["Timestamp", "kWh Produced", "kWh Used"]];

data[0][0] = "test";
console.log(data);

If you want to add a new value, you need to get the first position: data[0] and push the new value.

var data = [["Timestamp", "kWh Produced", "kWh Used"]];

data[0].push("test");
console.log(data);
Ele
  • 33,468
  • 7
  • 37
  • 75
-1

Thanks everyone.

Adding data[1] = [] before the data[1][0] = "test", as suggested by Andrew Boone, did the trick.

Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
Pfredd
  • 417
  • 6
  • 15