我在努力推动
data.push({"country": "IN"});
as new id and value to a json string. but it gives the following error
Uncaught TypeError: data.push is not a function
data{"name":"ananta","age":"15"}
谢谢你的回复
我在努力推动
data.push({"country": "IN"});
as new id and value to a json string. but it gives the following error
Uncaught TypeError: data.push is not a function
data{"name":"ananta","age":"15"}
谢谢你的回复
要使用数组的push函数,var必须是array.
将data{"name":"ananta","age":"15"}
更改为以下值:
var data = [
{
"name": "ananta",
"age": "15",
"country": "Atlanta"
}
];
data.push({"name": "Tony Montana", "age": "99"});
data.push({"country": "IN"});
..
包含的数组项将是typeof Object,您可以执行以下操作:
var text = "You are " + data[0]->age + " old and come from " + data[0]->country;
注意:尽量保持一致.在我的示例中,一个数组包含对象属性name
和age
,而另一个数组仅包含country
.如果我用for
或forEach
迭代,那么我不能总是判断一个属性,因为我的示例包含更改的项.
Perfect would be: 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
很酷的事情要知道:
var array = new Array();
类似于:
var array = [];
另外:
var object = new Object();
类似于:
var object = {};
您还可以将它们组合在一起:
var objectArray = [{}, {}, {}];