在Javascript中删除父数组

时间:2018-04-23 09:13:27

标签: javascript arrays

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

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

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

但我只需要这个,即只需要第一个对象的值。

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

我的代码如下:

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;

}

5 个答案:

答案 0 :(得分:0)

如果您的数组包含多个元素,则一个选项是使用 if (BuildConfig.FLAVOR_country.equals("saudi") && "ar".equals(LanguageUtil.getSelectedLanguageFromSharedPreferences(this))) { getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); } else { getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR); } concat

map

答案 1 :(得分:0)

如果你只需要jsonArray中的每个对象,你可以通过它的索引访问它。您只需要一个for循环来迭代每个对象。 我觉得你在找这样的东西。 创建一个新的空数组并将每个对象推送到它。

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

答案 2 :(得分:0)

与展开运算符forEach一起使用...以获得所需的输出。

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

答案 3 :(得分:0)

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

jsonModified = { hello: 'world' };

然后可以循环运行。

答案 4 :(得分:0)

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)