8

In the below code, I am getting "push is not a function" error. Can anyone please let me know what I am doing wrong here? I am trying to create 2D array in Javascript.

var myArray = new Array(4);
myArray = ["0","0","0","0"];

for (var i=0; i<myArray.length; i++) {
    myArray[i].push("ID");
    myArray[i] = new Array(1);
    for (var j=0; j<myArray[i].length; i++) {
        myArray[i][j].push("Array[j]");
    }
}

Firebug is pointing me to:

myArray[i].push("ID");

For this line I am getting "TypeError: myArray[i].push is not a function"

Final array it should look like is:

[ID,"SomeValue1"],
[ID,"SomeValue2"],
[ID,"SomeValue3"]

And I cannot hard code, I need to create this dynamically based on data from DB

2
  • 3
    myArray[i].push is attempting to add to whatever is in myArray[i], which in your case is "0" which is a string not an array and so has no .push ... But you then overwrite myArray[i] on the next line anyway ... got them the wrong way round? Commented Jan 10, 2013 at 12:17
  • looking again, this is all confusing, aside from the j loop incrementing i each time. I don't see the purpose of push("ID"). What should the array look like in the end? Commented Jan 10, 2013 at 12:22

3 Answers 3

6
myArray[i].push("ID");

Is invalid of course because it's a function defined for an Array not for it's elements.

valid syntax would be

myArray.push("ID");

To know more about array.push() go here.

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

Comments

5

This will create your example.

var myArray = new Array(4);
for (var i = 0; i < myArray.length; i++) {
  myArray[i] = ["ID", "SomeValue" + (i+1)];
}

But if you need to set data from a database, how is that being set in the Javascript? if it's in a different array you could do the following:

var dbArray = ["SomeValue1", "SomeValue2", "SomeValue3"];

var myArray = new Array(dbArray.length);
for (var i = 0; i < myArray.length; i++) {
  myArray[i] = ["ID", dbArray[i]];
}

1 Comment

Working great with some minor modifications. Thanks
3

First of all, you can initialize Arrays using the Array Literal [] which should always be preferred

Then push is an Array method. So you have to call it on an Array

myArray[i] is the Element of the Array, myArray the Array

var arr = [];

for (var i = 0; i  < 5;i++) {
  arr.push = []; //Uses the Arrays push method
  for ( var j = 0; j < 5; j++)
    arr[i][j] = "Asd"; //Sets an Element
}
  console.log(arr);

And as you see don't need to "Dim" an Array, you just can assign an Array to a Variable and start pushing Elements in it.

As you can see in the example, the first time

push is directly after arr, its the Arrays Method and appends a new Element to the Array

And in the second Example it accesses the Element and assigns it a value directly


What you are doing in your code is trying to invoke the push method on an Elements Array

Assume i is 0 Then

'myArray[i]' is "0" and the String "0" has no Method push.

But you can directly assing a new String to the Element like.

myArray[i] = "ID"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.