gpt4 book ai didi

javascript - 在 Javascript 中格式化时间、金额

转载 作者:太空宇宙 更新时间:2023-11-03 22:23:36 27 4
gpt4 key购买 nike

我的数据就是这样返回的。我需要返回列表中的所有值,确保时间格式和票价金额按照我的解释进行纠正。我想删除票价中的逗号以及出发和到达中的 AM & PM。提前谢谢了。因为大约有 3 个航类代码,总共有 18 种票价。

返回的响应。

[ { 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' } ]

预期结果:

[ { 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' }
]

我的 Javascript 代码如下。

 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;

最佳答案

使用split(' ')[0]删除时间参数AM/PMreplace(',','') 替换票价中的逗号。

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);

关于javascript - 在 Javascript 中格式化时间、金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50285760/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com