0

I am trying to insert/push values into a blank multidimensional array.

I am having trouble doing it. My code is

var online = [];
//already used the  = new Array();

//My Method 1:
online.push({'1921020309','ALLIED BANK','Savings Account'});

//My Method 2:
online[0][0] = '1921020309';
online[0][1] = 'ALLIED BANK';
online[0][2] = 'Savings Account';

document.getElementById("demo").innerHTML = online;

What I am doing wrong here? Thanks for the help.

Lee Balino
  • 490
  • 2
  • 12
  • Method 2 looks good. Does it give an error? – John Doe Dec 04 '15 at 02:36
  • @JohnDoe Yup dude it says `Uncaught SyntaxError: Unexpected identifier` – Lee Balino Dec 04 '15 at 02:37
  • Do either of these help? http://stackoverflow.com/questions/7545641/javascript-multidimensional-array. http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – John Doe Dec 04 '15 at 02:41
  • In method 1, your syntax is wrong, array literals use square parenthesis: `['1921020309','ALLIED BANK','Savings Account']`. Curly braces are for Object literals: `{key:value,...}`. – RobG Dec 04 '15 at 02:45
  • Just found the right codes. `var online = new Array(2); online[0] = new Array('1921020309', 'ALLIED BANK', 'Savings Account'); online[1] = new Array('028-00-000831-3', 'Bank of Commerce', 'Auto Transfer Account'); document.getElementById("demo").innerHTML = online;` – Lee Balino Dec 04 '15 at 02:55
  • @JohnDoe Thanks for the Hint. – Lee Balino Dec 04 '15 at 02:58
  • @LeeBalino—you don't need to set the length when creating an array, it's self–adjusting. It is generally preferred to use an array literal (e.g. `[valu0, value1, ...]`) rather than the using the Array constructor: `new Array(value0, value1, ...)`. – RobG Dec 04 '15 at 03:34

2 Answers2

1

You need to make a new array for every dimension:

online[0] = []; // <-
online[0][0] = '1921020309';
online[0][1] = 'ALLIED BANK';
online[0][2] = 'Savings Account';
deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • Tried this man but it says `Uncaught SyntaxError: Unexpected token [` – Lee Balino Dec 04 '15 at 02:48
  • I assumed that you still have `var online = [];` at the top and nothing else. – deamentiaemundi Dec 04 '15 at 02:51
  • No dude I change that to what you have gave `online[0] = [];` – Lee Balino Dec 04 '15 at 02:57
  • Just already found the right combination of codes `var online = new Array(2); online[0] = new Array('1921020309', 'ALLIED BANK', 'Savings Account'); online[1] = new Array('028-00-000831-3', 'Bank of Commerce', 'Auto Transfer Account'); document.getElementById("demo").innerHTML = online;` I am putting it to the answers. Thanks dude. – Lee Balino Dec 04 '15 at 02:58
1

I just found the right answer to my question

var online = new Array(2);
online[0] = new Array('1921020309', 'ALLIED BANK', 'Savings Account');
online[1] = new Array('028-00-000831-3', 'Bank of Commerce', 'Auto Transfer Account');

document.getElementById("demo").innerHTML = online;

Now all works fine.

Lee Balino
  • 490
  • 2
  • 12