推送不是函数 JavaScript 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14257709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:11:26  来源:igfitidea点击:

Push is not a function JavaScript error

javascript

提问by

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.

在下面的代码中,我收到“推送不是函数”错误。谁能让我知道我在这里做错了什么?我正在尝试在 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:

Firebug 指向我:

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

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

对于这一行,我收到“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

而且我不能硬编码,我需要根据数据库中的数据动态创建它

采纳答案by Rhumborl

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:

但是,如果您需要从数据库中设置数据,那么在 Javascript 中是如何设置的呢?如果它在不同的数组中,您可以执行以下操作:

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

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

回答by yogi

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

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

当然是无效的,因为它是一个为Arraynot 元素定义的函数。

valid syntax would be

有效的语法是

myArray.push("ID");

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

要了解更多信息,array.push()请访问这里

回答by Moritz Roessler

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

首先,您可以使用 Array Literal 初始化数组,[]这应该是首选

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

然后push是一个Array方法。所以你必须在一个数组上调用它

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

myArray[i]是数组的元素,myArray数组

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

正如您在示例中看到的,第一次

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

push紧跟其后arr,它的 Arrays 方法并将一个新元素附加到 Array

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

在第二个示例中,它访问 Element 并直接为其赋值



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

您在代码中所做的是尝试在 Elements Array 上调用 push 方法

Assume iis 0Then

假设i0那么

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

'myArray[i]' 是"0"并且字符串"0"没有方法推送。

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

但是您可以直接将新字符串分配给 Element 之类的。

myArray[i] = "ID"

myArray[i] = "ID"