简体   繁体   中英

Remove Parent Array In Javascript

I have to return array only in this format. How to acheive that. Thanks in advance.

My result is returned in this way.

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

but I need this only ie only the value of first object.

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

and my code is below:

const cheerio = require('cheerio');

export default function parseHtml(html) {

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: '0'
             }
        })
    });
    console.log(JSON.stringify(result));
return result;

}

If your array has more than one element, One option is to use concat and map

 let arr = [{ "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" }] }] let result = [].concat(...arr.map(o => o.klasses)); console.log(result); 

If you only every need 1 object in a jsonArray, you access it through its index. All you need is a for loop to iterate over each object. I think your looking for something like this. Create a new empty array and push each object to it.

newArry=[]
for(var i=0; i<flights.length; i++;){
    singleObject = flights[i];
    newArr.push(singleObject);
}
console.log(newArr);

Use forEach alongside with spread operator ... to get the desired output.

 var arr = [{ "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" }] }] var res = []; arr.forEach((obj)=>{ res.push(...obj.klasses); }); console.log(res); 

var jsonOriginal = { hw: { hello: 'world' } };
var jsonModified = jsonOriginal.hw;

will then be

jsonModified = { hello: 'world' };

This then could be run over a loop.

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


console.log(arr[0].klasses) 

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.

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