My data is returned in this way. I need to return all the values in a list ensuring the time format and fare amount corrected as I explained. I want to remove the comma in fare, and AM & PM in departure and arrival. Many thanks in advance. As there are about 3 flightCodes and all together 18 fares.
The returned response.
[ { flightCode: 'SHA735',
departure: '01:30 PM',
arrival: '01:55 PM',
fareCode: 'T Class',
fare: '4,000',
baggage: '20' },
{ flightCode: 'SHA735',
departure: '01:30 PM',
arrival: '01:55 PM',
fareCode: 'B Class',
fare: '5,900',
baggage: '20' },
{ flightCode: 'SHA735',
departure: '01:30 PM',
arrival: '01:55 PM',
fareCode: 'I Class',
fare: '6,600',
baggage: '20' },
{ flightCode: 'SHA735',
departure: '01:30 PM',
arrival: '01:55 PM',
fareCode: 'H Class',
fare: '7,500',
baggage: '20' },
{ flightCode: 'SHA735',
departure: '01:30 PM',
arrival: '01:55 PM',
fareCode: 'H Class',
fare: '7,500',
baggage: '20' } ]
Expected Result:
[ { flightCode: 'SHA735',
departure: '01:30',
arrival: '01:55',
fareCode: 'T Class',
fare: '4000',
baggage: '20' },
{ flightCode: 'SHA735',
departure: '01:30',
arrival: '01:55',
fareCode: 'S Class',
fare: '5000',
baggage: '20' },
{ flightCode: 'SHA735',
departure: '01:30',
arrival: '01:55',
fareCode: 'E Class',
fare: '5300',
baggage: '20' },
{ flightCode: 'SHA735',
departure: '01:30',
arrival: '01:55',
fareCode: 'B Class',
fare: '5900',
baggage: '20' }
]
My Javascript code is below.
const $ = cheerio.load(html);
const format = {
klasses: new Array(),
}
const result = [];
const flights = $('.flight-result > .tbody').find('.no-of-flights').toArray();
flights.forEach(function(flight, _id) {
result[_id] = Object.assign({},format);
const flightCode = $(flight).find('p.font-reg.redcolor').contents().filter(function(){
return this.type === 'text';
}).text().trim();
const times = $(flight).find('p.font-reg.redcolor').find('small').html().match(/\d\d:\d\d [AP]M/g);
const flightClassContainer = $(flight).find('.flightclasscontainer').toArray();
flightClassContainer.forEach((flightClass, __id) => {
result[_id].klasses[__id] = {
flightCode:$(flight).find('p.font-reg.redcolor').contents().filter(function () {
return this.type === 'text';
}).text().trim(),
departure: times[0],
arrival: times[1],
fareCode: $(flightClass).find('.class').text(),
fare: $(flightClass).find('.price').text(),
baggage: '20'
}
})
});
let items = [].concat(...result.map(o => o.klasses));
console.log(items);
return items;
Use split(' ')[0]
to remove the time parameter AM/PM
and replace(',','')
to replace the comma in fare.
var flights = [ { flightCode: 'SHA735', departure: '01:30 PM', arrival: '01:55 PM', fareCode: 'T Class', fare: '4,000', baggage: '20' }, { flightCode: 'SHA735', departure: '01:30 PM', arrival: '01:55 PM', fareCode: 'B Class', fare: '5,900', baggage: '20' }, { flightCode: 'SHA735', departure: '01:30 PM', arrival: '01:55 PM', fareCode: 'I Class', fare: '6,600', baggage: '20' }, { flightCode: 'SHA735', departure: '01:30 PM', arrival: '01:55 PM', fareCode: 'H Class', fare: '7,500', baggage: '20' }, { flightCode: 'SHA735', departure: '01:30 PM', arrival: '01:55 PM', fareCode: 'H Class', fare: '7,500', baggage: '20' } ]; flights.map((flight) => { flight.departure = flight.departure.split(' ')[0]; flight.arrival = flight.arrival.split(' ')[0]; flight.fare = flight.fare.replace(',',''); }); console.log(flights);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.