Я должен возвращать массив только в этом формате. Как добиться этого. Заранее спасибо.
Мой результат возвращается таким образом.
[{"klasses":[{"flightCode":"SHA735","departure":"03:50 PM","arrival":"04:15 PM","fareCode":"T Class","fare":"4,000","baggage":"0"},{"flightCode":"SHA735","departure":"03:50 PM","arrival":"04:15 PM","fareCode":"S Class","fare":"5,000","baggage":"0"},{"flightCode":"SHA735","departure":"03:50 PM","arrival":"04:15 PM","fareCode":"E Class","fare":"5,300","baggage":"0"}]}]
но мне нужно это только исключая информацию о AM / PM и запятую между суммой тарифа.
[{"flightCode":"SHA735","departure":"03:50","arrival":"04:15","fareCode":"T Class","fare":"4000","baggage":"0"},{"flightCode":"SHA735","departure":"03:50","arrival":"04:15","fareCode":"S Class","fare":"5000","baggage":"0"},{"flightCode":"SHA735","departure":"03:50","arrival":"04:15","fareCode":"E Class","fare":"5300","baggage":"10"}]
и мой код ниже:
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;
}