javascript 如何在jquery中推送这样的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22283175/
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
how to push an object like this in jquery?
提问by Vincent815
may I know how to push var obj= [{}]
in .each
? for example like this.
我可能知道如何推动var obj= [{}]
中.each
?例如像这样。
$.each(maintasks_row, function(index, maintasks_val) {
obj.push([{
"name" : maintasks_val.task_name,
"desc" : "",
"values" : [{
"from" : "/Date("+maintasks_val.time_start+")/",
"to" : "/Date("+maintasks_val.time_end+")/",
"label": maintasks_val.task_name,
"customClass" : "ganttRed"
}]
}]);
});
I'm using this for $(".gantt").gantt({source: obj});
我用这个 $(".gantt").gantt({source: obj});
On this sitethe var data
is [{}]
is this an object? and how can I insert it?
在这个网站上,这var data
是[{}]
一个对象吗?我怎样才能插入它?
thank you
谢谢
回答by Rob Sedgwick
.push
does not require you delcare it as an array ( like you tried obj.push([{
- unless of course you are pushing an array into an array
.push
不需要你把它作为一个数组(就像你试过的那样obj.push([{
- 除非你当然要把一个数组推到一个数组中
Just simply ...
简直...
obj.push({"name" : maintasks_val.task_name, ..
adds a new single item intto the array
在数组中添加一个新的单个项目
Update as comment ,yes, declare obj
as a typeof
array first
更新为注释,是的,先声明obj
为typeof
数组
var obj=[];
This is now the same as the data
array you have shown in your docs example- and we can now .push()
into it.
这现在与data
您在文档示例中显示的数组相同- 我们现在可以.push()
进入它。