在js中推送数组看起来不能正常工作

时间:2012-10-21 22:08:36

标签: php javascript twig

 $Flight = array
            (
            '1' => array
                (
                '5' => array
                    (
                    'LFID' => '6684',
                    'DepartureDate' => '2012-10-22T12:30:00',
                    'ArrivalDate' => '2012-10-22T14:00:00',
                    'BaseFareAmtInclTax' => '343.50',
                    'ReturnFlightSegmentDetails' =>
                    array
                        (
                        '87' => array
                            (
                            'LFID' => '6647',
                            'DepartureDate' => '2012-10-24T12:25:00',
                            'FareTypeName' => 'Change',
                            'FareTypeID' => '22',
                            'BaseFareAmtInclTax' => '198',
                        )
                    )
                ),
                '10' => array
                    (
                    'LFID' => '6684',
                    'DepartureDate' => '2012-10-23T12:30:00',
                    'ArrivalDate' => '2012-10-23T14:00:00',
                    'BaseFareAmtInclTax' => '243.50',
                    'ReturnFlightSegmentDetails' =>
                    array
                        (
                        '150' => array
                            (
                            'LFID' => '6647',
                            'DepartureDate' => '2012-10-24T12:25:00',
                            'FareTypeName' => 'Change',
                            'FareTypeID' => '22',
                            'BaseFareAmtInclTax' => '198',
                        ),
                        '153' => Array
                            (
                            'LFID' => '6647',
                            'DepartureDate' => '2012-10-26T12:25:00',
                            'FareTypeName' => 'Change',
                            'FareTypeID' => '22',
                            'BaseFareAmtInclTax' => '198',
                        )
                    )
                )
            ),
        );

我有来自我的控制器的这个数组传递给我的树枝,我执行下面的代码将其解析为js数组:

{% for flight in flights%}
                   var $FlightSegment = new Array();
                  $flight.push({
                        key:"{{ flight.key }}",
                        DepartureDate:"{{flight.DepartureDate}}",
                        ArrivalDate:"{{flight.ArrivalDate}}",
                        Amount:"{{flight.BaseFareAmtInclTax}}",

                     });
                     {% for ReturnFlightSegmentDetails in flight.ReturnFlightSegmentDetails %}
                          $FlightSegment.push({
                                LFID :"{{ReturnFlightSegmentDetails.LFID}}", 
                                DepartureDate:"{{ReturnFlightSegmentDetails.DepartureDate}}",
                                ArrivalDate:"{{ReturnFlightSegmentDetails.BaseFareAmtInclTax}}",
                                Class:"{{ReturnFlightSegmentDetails.FareTypeName}}",
                           });
                      {% endfor %}
                      console.info($FlightSegment);
                    $flight.push({
                        ReturnFlightSegmentDetails :$FlightSegment,
                    });
                {% endfor %} 

问题是$ flight.length返回4而不是2:/ ReturnFlightSegmentDetails被视为对象而不是飞行的attibut:/

1 个答案:

答案 0 :(得分:0)

您需要以不同方式构建“飞行”对象;实际上不需要“$ FlightSegment”变量。

{% for flight in flights%}
    $flight.push({
        key: "{{ flight.key }}",
        DepartureDate: "{{flight.DepartureDate}}",
        ArrivalDate: "{{flight.ArrivalDate}}",
        Amount: "{{flight.BaseFareAmtInclTax}}",
        ReturnFlighSegmentDetails: [
           {% for ReturnFlightSegmentDetails in flight.ReturnFlightSegmentDetails %}
             {
                LFID :"{{ReturnFlightSegmentDetails.LFID}}", 
                DepartureDate:"{{ReturnFlightSegmentDetails.DepartureDate}}",
                ArrivalDate:"{{ReturnFlightSegmentDetails.BaseFareAmtInclTax}}",
                Class:"{{ReturnFlightSegmentDetails.FareTypeName}}"
              }
               {% if not loop.last } , {% endif }
           {% endfor %}
        ]
       });
 {% endfor %}