1

can you please take a look at this snippet and let me know why I am not able to create an array of array at this example?

var data1 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data2 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data3 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data4 = [5555,22,102858,12,.554,88888,99999999,12,1.5];

var all = [];
all[0].push(data1);
all[1].push(data2);
all[2].push(data3);
all[3].push(data4);

var myJsonString = JSON.stringify(all);
console.log(myJsonString);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

2
  • For future reference, try to look at the cause of these types of behavior earlier. If you had done console.log(all[0]) it would have been pretty evident what was going on. Commented Sep 18, 2015 at 19:02
  • For future reference (2), read the error console. (It would have thrown a JavaScript error on all[0].push - if nothing else this sort of 'failing' message should be reported in diagnostics/questions.) Commented Sep 18, 2015 at 19:08

2 Answers 2

6

You should be doing all.push(dataX) since all[X] is undefined.

var data1 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data2 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data3 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data4 = [5555,22,102858,12,.554,88888,99999999,12,1.5];

var all = [];
all.push(data1);
all.push(data2);
all.push(data3);
all.push(data4);

var myJsonString = JSON.stringify(all);
console.log(myJsonString);

Explanation:

In the code you wrote, all is an empty Array.

all[X] (e.g. all[0]) refers to the first element of the array, which doesn't exist (since array is empty). JavaScript interprets this as undefined. (There is no array out of bounds exception)

undefined has no method .push(...) which is why your code is failing.

What you want is to call all.push(...) where you're calling .push(...) on the all Array.

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

2 Comments

Thanks arc but kinda confusing! why isn't the all acting like all other arrays?!
If you want to access the array by index, you can write all[0] = data1;. Generally, it's considered cleaner to use other language features so you don't have to rely on perfect hand-counting of indices.
2
all[0].push(data1);

all[0] retrieves the first element of all. Since you just made the array and it's empty, this is undefined. (In other languages, it would be an array index access violation). You're then trying to push onto that first element; if it were an array itself, this would give you [[[5555,22,... which is one more array container than you want.

Easiest solution is to construct the array via a literal.

var all = [data1, data2, data3, data4];
// no "push()" necessary here.

Comments

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.