admin管理员组

文章数量:1289634

may I know how to push var obj= [{}] in .each? for example like this.

 $.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});

On this site the var data is [{}] is this an object? and how can I insert it?

thank you

may I know how to push var obj= [{}] in .each? for example like this.

 $.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});

On this site the var data is [{}] is this an object? and how can I insert it?

thank you

Share Improve this question edited Mar 9, 2014 at 15:32 zoranc 2,4561 gold badge22 silver badges34 bronze badges asked Mar 9, 2014 at 14:05 Vincent815Vincent815 1391 gold badge2 silver badges9 bronze badges 3
  • 1 You're adding single-element arrays prising an object onto obj here; is that what you want? – Ja͢ck Commented Mar 9, 2014 at 14:08
  • You said you want to push object but inserting array of object, what you want to do? – Suman Bogati Commented Mar 9, 2014 at 14:09
  • yes, i want to insert it like this var data = [ { "name": " Step A ","desc": "&rarr; Step B" ,"values": [{"id": "b0", "from": "/Date(1320182000000)/", "to": "/Date(1320301600000)/", "desc": "Id: 0<br/>Name: Step A", "label": " Step A", "customClass": "ganttRed", "dep": "b1"}] }]; – Vincent815 Commented Mar 9, 2014 at 14:09
Add a ment  | 

1 Answer 1

Reset to default 7

.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

Just simply ...

obj.push({"name" : maintasks_val.task_name, ..

adds a new single item intto the array


Update as ment , yes, declare obj as a typeof array first

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.

本文标签: javascripthow to push an object like this in jqueryStack Overflow