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.

7
  • Method 2 looks good. Does it give an error? Commented Dec 4, 2015 at 2:36
  • @JohnDoe Yup dude it says Uncaught SyntaxError: Unexpected identifier Commented Dec 4, 2015 at 2:37
  • Do either of these help? stackoverflow.com/questions/7545641/…. stackoverflow.com/questions/966225/… Commented Dec 4, 2015 at 2: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,...}. Commented Dec 4, 2015 at 2: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; Commented Dec 4, 2015 at 2:55

2 Answers 2

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';
Sign up to request clarification or add additional context in comments.

4 Comments

Tried this man but it says Uncaught SyntaxError: Unexpected token [
I assumed that you still have var online = []; at the top and nothing else.
No dude I change that to what you have gave online[0] = [];
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.