在JS中格式化数据,删除AM / PM信息&逗号金额

时间:2018-04-25 09:56:21

标签: javascript web-scraping cheerio

我必须以这种格式返回数组。如何实现这一目标。提前谢谢。

我的结果以这种方式返回。

[{"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;

}

2 个答案:

答案 0 :(得分:1)

您可以使用数组map()

执行以下操作



var data = [{"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"}]}]

data = data[0].klasses.map(function(d){
    d.departure = d.departure.split(' ')[0];
    d.arrival = d.arrival.split(' ')[0];
    d.fare = d.fare.replace(',', '');
    return d;
});
console.log(data)




答案 1 :(得分:0)

您可以从此代码中获取帮助。使用splitreplace等方法来获取:



var jsonData = [{"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"}]}];

jsonData[0].klasses.forEach((klass)=>{
  klass.departure = klass.departure.split(' ')[0];
  klass.arrival = klass.departure.split(' ')[0];
  klass.fare = klass.fare.replace(',','');
});

console.log(jsonData);