最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Push is not a function JavaScript error - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited May 19, 2016 at 7:51 Pierre de LESPINAY 46.2k58 gold badges218 silver badges314 bronze badges asked Jan 10, 2013 at 12:14 user1702383user1702383 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? – Alex K. 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? – Rhumborl Commented Jan 10, 2013 at 12:22
Add a comment  | 

3 Answers 3

Reset to default 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.

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]];
}

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"

发布评论

评论列表(0)

  1. 暂无评论