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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:50:42  来源:igfitidea点击:

how to push an object like this in jquery?

javascriptphpjqueryhtmlgantt-chart

提问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 datais [{}]is this an object? and how can I insert it?

这个网站上,var data[{}]一个对象吗?我怎样才能插入它?

thank you

谢谢

回答by Rob Sedgwick

.pushdoes 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 objas a typeofarray first

更新为注释,是的,先声明objtypeof数组

var obj=[];

This is now the same as the dataarray you have shown in your docs example- and we can now .push()into it.

这现在与data您在文档示例中显示的数组相同- 我们现在可以.push()进入它。