Skip to content
Advertisement

Pushing an array in js doesn’t look to work properly

 $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',
                        )
                    )
                )
            ),
        );

I have this array from my controller which is passed to my twig where I execute the below code to parse it into a js array:

{% 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 %} 

The problem is that $flight.length returns 4 instead of 2, the ReturnFlightSegmentDetails is considered like an object instead to be an attribute for a flight.

Advertisement

Answer

You need to build the “flight” object differently; there’s really no need for the “$FlightSegment” variable.

{% 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 %} 
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement