简体   繁体   English

格式化时间,JavaScript量

[英]Formatting Time, Amount In Javascript

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. 我要删除票价中的逗号,以及出发和到达时的AM和PM。 Many thanks in advance. 提前谢谢了。 As there are about 3 flightCodes and all together 18 fares. 因为大约有3个航班代码,总共18个票价。

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. 我的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;

Use split(' ')[0] to remove the time parameter AM/PM and replace(',','') to replace the comma in fare. 使用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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM