1
 $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 被认为是一个对象,而不是一个航班的属性。

4

1 回答 1

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 %} 
于 2012-10-21T22:16:19.460 回答