Javascript 未捕获的类型错误:data.push 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29696572/
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-23 03:48:50  来源:igfitidea点击:

Uncaught TypeError: data.push is not a function

javascriptjqueryjson

提问by Ananta Prasad

I am trying to push

我正在努力推动

data.push({"country": "IN"});

as new id and value to a json string. but it gives the following error

作为 json 字符串的新 id 和值。但它给出了以下错误

Uncaught TypeError: data.push is not a function

data{"name":"ananta","age":"15"}

Advance Thanks for your reply

提前感谢您的回复

回答by Cagatay Ulubay

To use the push function of an Array your var needs to be an Array.

要使用数组的推送功能,您的 var 需要是一个数组。

Change data{"name":"ananta","age":"15"}to following:

更改data{"name":"ananta","age":"15"}到以下几点:

var data = [
    { 
        "name": "ananta",
        "age": "15",
        "country": "Atlanta"
    }
];

data.push({"name": "Tony Montana", "age": "99"});

data.push({"country": "IN"});

..

The containing Array Items will be typeof Object and you can do following:

包含的数组项将是 typeof Object,您可以执行以下操作:

var text = "You are " + data[0]->age + " old and come from " + data[0]->country;

var text = "You are " + data[0]->age + " old and come from " + data[0]->country;

Notice: Try to be consistent. In my example, one array contained object properties nameand agewhile the other only contains country. If I iterate this with foror forEachthen I can't always check for one property, because my example contains Items that changing.

注意:尽量保持一致。在我的示例中,一个数组包含对象属性nameage而另一个只包含country. 如果我用forforEach然后迭代它,我不能总是检查一个属性,因为我的示例包含更改的项目。

Perfect would be: data.push({ "name": "Max", "age": "5", "country": "Anywhere" } );

完美的应该是: data.push({ "name": "Max", "age": "5", "country": "Anywhere" } );

So you can iterate and always can get the properties, even if they are empty, null or undefined.

因此,您可以迭代并始终​​可以获得属性,即使它们是空的、空的或未定义的。

edit

编辑

Cool stuff to know:

很酷的东西要知道:

var array = new Array();

is similar to:

类似于:

var array = [];

Also:

还:

var object = new Object();

is similar to:

类似于:

var object = {};

You also can combine them:

你也可以组合它们:

var objectArray = [{}, {}, {}];

回答by Rory McCrossan

Your datavariable contains an object, not an array, and objects do not have the pushfunction as the error states. To do what you need you can do this:

您的data变量包含一个对象,而不是一个数组,并且对象不具有push错误状态的功能。要做你需要的,你可以这样做:

data.country = 'IN';

Or

或者

data['country'] = 'IN';

回答by Palo

Also make sure that the name of the variable is not some kind of a language keyword. For instance, the following produces the same type of error:

还要确保变量的名称不是某种语言关键字。例如,以下会产生相同类型的错误:

var history = [];
history.push("what a mess");

replacing it for:

将其替换为:

var history123 = [];
history123.push("pray for a better language");

works as expected.

按预期工作。

回答by rvandoni

you can use push method only if the object is an array:

仅当对象是数组时才可以使用 push 方法:

var data = new Array();
data.push({"country": "IN"}).

OR

或者

data['country'] = "IN"

if it's just an object you can use

如果它只是一个你可以使用的对象

data.country = "IN";

回答by asalam345

I think u set it

我想你设置了

var data = []; 

but after some times u make it

但过了一段时间你成功了

data = 'some things'; 

then data.push('')not work.

然后 data.push('')不工作。

回答by Jayaraman M

Try This Code $scope.DSRListGrid.data = data; this one for source data

试试这个代码 $scope.DSRListGrid.data = data; 这是源数据

            for (var prop in data[0]) {
                if (data[0].hasOwnProperty(prop)) {
                    $scope.ListColumns.push(
                            {
                                "name": prop,
                                "field": prop,
                                "width": 150,
                                "headerCellClass": 'font-12'
                            }
                    );
                }
            }
            console.log($scope.ListColumns);

回答by Nilesh Bandekar

make sure you pushinto an Array only and if their is error like Uncaught TypeError: data.pushis not a function** then check for type of datayou can do this by consol.log(data)hope this will help

确保你只入一个数组,如果它们是像Uncaught TypeError: data.pushis not a function** 这样的错误,然后检查 你可以通过consol.log(data)执行此操作的数据类型 希望这会有所帮助