Javascript 循环遍历数据,创建一个数组并作为json文件输出

Javascript 循环遍历数据,创建一个数组并作为json文件输出,javascript,arrays,json,cheerio,Javascript,Arrays,Json,Cheerio,我想循环使用cheerio收集的数据 输入数据示例: 11 aug 2017 Arsenal - Leicester City 12 aug 2017 Watford - Liverpool Crystal Palace - Huddersfield Town Everton - Stoke City 我想创建一个json文件作为最终结果: { "fixtureDate": [ { "homeTeam": "Ar

我想循环使用cheerio收集的数据

输入数据示例:

11 aug 2017
    Arsenal - Leicester City
12 aug 2017 
    Watford - Liverpool
    Crystal Palace - Huddersfield Town
    Everton - Stoke City
我想创建一个json文件作为最终结果:

{
    "fixtureDate": [
        {
            "homeTeam": "Arsenal",
            "awayTeam": "Leicester City",
            "matchDate": " 11 aug 2017 "
        },
        {
            "homeTeam": "Watford",
            "awayTeam": "Liverpool",
            "matchDate": " 12 aug 2017 "
        },
        {
            "homeTeam": "Crystal Palace",
            "awayTeam": "Huddersfield Town",
            "matchDate": " 12 aug 2017 "
        },
        {
            "homeTeam": "Everton",
            "awayTeam": "Stoke City",
            "matchDate": " 12 aug 2017 "
        },  
代码现在必须循环遍历数据并创建一个数组:

// loop trough the data
for(var i=0; i<json.matchDate.length; i++){
    output.fixtureDate[i] = {
        matchDate : json.matchDate[i], 
        homeTeam : json.homeTeam[i],
        awayTeam : json.awayTeam[i],              
        matchTime : json.matchTime[i]
    }

}  
如何循环数据并创建正确的数组

到目前为止,我已经创建了完整的代码:

var cheerio = require('cheerio');
var request = require('request');
var fs = require('fs');

var url = 'FIXTURES LINK'; 

request(url, function (error, response, html) {

  if (!error && response.statusCode == 200) {

    var $ = cheerio.load(html);  

    // create json structure in an array with the scraped data
    var json = {
      homeTeam : [],
      awayTeam : [],
      matchTime : [],
      matchDate : []       
    };
    output = {
      fixtureDate : []
    };    


    // create homeTeam value from website
    $('.fm-fixtures__list li .fm-fixture .fm-fixture__team--home .fm-fixture__team__name').each(function(){
      json.homeTeam.push($(this).text());
    });

    // create awayTeam value from website
    $('.fm-fixtures__list li .fm-fixture .fm-fixture__team--away .fm-fixture__team__name').each(function(){
      json.awayTeam.push($(this).text());
    });   

    // create matchTime value from website
    $('.fm-fixtures__list li .fm-fixture .fm-fixture__status .match-status').each(function(){
      json.matchTime.push($(this).text());
    }); 

    // create matchDate value from website
    $('.fm-fixtures__list li .fm-fixtures__date').each(function(){
      json.matchDate.push($(this).text());
    });                       


    // loop trough the data
    for(var i=0; i<json.homeTeam.length; i++){
        output.fixtureDate[i] = {
            matchDate : json.matchDate[i], 
            homeTeam : json.homeTeam[i],
            awayTeam : json.awayTeam[i],              
            matchTime : json.matchTime[i]
        }

    }  

    // create a json output and print in the console
    var scrape = JSON.stringify(output, null, 4);
    console.log(scrape);

    // create a json file 
    fs.writeFile('fixtures.json', JSON.stringify(output, null, 4), function(err){
        console.log('File successfully written to folder!');
    })            

  } // end if error
}); // end request function
var cheerio=require('cheerio');
var请求=要求(“请求”);
var fs=需要('fs');
var url='FIXTURES LINK';
请求(url、函数(错误、响应、html){
如果(!error&&response.statusCode==200){
var$=cheerio.load(html);
//在一个数组中创建json结构,其中包含已刮取的数据
var json={
主队:[],
awayTeam:[],
比赛时间:【】,
比赛日期:[]
};
输出={
固定日期:[]
};    
//从网站创建家庭团队价值
$('.fm-fixtures\uuu列表li.fm fixture.fm-fixture\uuuu团队--home.fm-fixture\uuu团队名称')。每个(函数(){
json.homeTeam.push($(this.text());
});
//从网站创建awayTeam价值
$('.fm-fixtures\uuu list li.fm fixture.fm-fixture\uuu team--away.fm-fixture\uu team\uu name')。每个(函数(){
json.awayTeam.push($(this.text());
});   
//从网站创建matchTime价值
$('.fm-fixtures\uuu list li.fm fixture.fm-fixture\uuu status.match status')。每个(函数(){
json.matchTime.push($(this.text());
}); 
//从网站创建matchDate值
$('.fm-fixtures\uu list li.fm-fixtures\uu date')。每个(函数(){
json.matchDate.push($(this.text());
});                       
//通过数据循环

对于(var i=0;i,由于匹配项不是按日期分组的(在HTML中),您必须抓取每个匹配项,并检查它是否有日期标签。如果有,请使用其日期并将其推送到数组中。如果没有,请使用上次使用的日期作为该匹配项的日期,如下所示:

注意:您也可以在一个循环中执行此操作,而不是在多个循环中创建多个数组,然后组合它们

//从网站创建匹配日期值
var json={
“固定日期”:[]
};
var lastDate=“”;
$('.fm-fixtures__listli')。每个(函数(){
var matchContainer=$(这个);
var homeTeam=matchContainer.find(“.fm-fixture\uuuuu team--home.fm-fixture\uuuuuu team\uuuu name”).text().trim();
var awayTeam=matchContainer.find(“.fm-fixture\uuuuu team--awe.fm-fixture\uuuuuu team\uuuu name”).text().trim();
var matchDateContainer=matchContainer.find(“.fm-fixtures__日期__标签”);
var matchDate=“”;
if(matchDateContainer.length){
lastDate=matchDateContainer.text().trim();
}
matchDate=lastDate;
json.fixtureDate.push({homeTeam:homeTeam,awayTeam:awayTeam,matchDate:matchDate});
}); 
console.log(json);

这样做可以:

var cheerio = require('cheerio');
var request = require('request');
var fs = require('fs');

var url = "https://www.fotmob.com/leagues/47/matches/" 

request(url, function (error, response, html) {

  if (!error && response.statusCode == 200) {

    var $ = cheerio.load(html);  

    let matchDate;

    let jsonArray = $('#app-view > div.fm-wrapper > section > div.fm-content > section ul > li').map(function(i, el) {
        // this === el
        let htmlLi = $(this).html();
        $ = cheerio.load(htmlLi);
    let home = $('div.fm-fixture > div.fm-fixture__team.fm-fixture__team--home > p').text() || 'emptyRes';
    let away = $('div.fm-fixture > div.fm-fixture__team.fm-fixture__team--away > p').text();
    let status = $('div > div.fm-fixture__status > span').text();
    let date = $('.fm-fixtures__date__label').text() || "useSameDate";

    if (date != "useSameDate"){
        matchDate = date;
    }

    if (home == "emptyRes"){
        return;
    }

        let matchDataObj = {
            matchDate : matchDate,
            homeTeam : home,
            awayTeam: away,
            matchStatus: status

        }
        // console.log(matchDataObj)
        return matchDataObj;
      }).toArray();

    var JsonData = {
        fixtureDate : jsonArray
    };

    // create a json file 
    fs.writeFile('fixtures.json', JSON.stringify(JsonData, null, 4), function(err){
        console.log('File successfully written to folder!');
    })            

  } // end if error
}); // end request function
fixtures.json的输出将是:

{
    "fixtureDate": [
        {
            "matchDate": "August 11, 2017  ",
            "homeTeam": "Arsenal",
            "awayTeam": "Leicester City",
            "matchStatus": " 4 : 3 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Watford",
            "awayTeam": "Liverpool",
            "matchStatus": " 3 : 3 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Crystal Palace",
            "awayTeam": "Huddersfield Town",
            "matchStatus": " 0 : 3 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Everton",
            "awayTeam": "Stoke City",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "West Bromwich Albion",
            "awayTeam": "AFC Bournemouth",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Chelsea",
            "awayTeam": "Burnley",
            "matchStatus": " 2 : 3 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Southampton",
            "awayTeam": "Swansea City",
            "matchStatus": " 0 : 0 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Brighton & Hove Albion",
            "awayTeam": "Manchester City",
            "matchStatus": " 0 : 2 "
        },
        {
            "matchDate": "August 13, 2017  ",
            "homeTeam": "Newcastle United",
            "awayTeam": "Tottenham Hotspur",
            "matchStatus": " 0 : 2 "
        },
        {
            "matchDate": "August 13, 2017  ",
            "homeTeam": "Manchester United",
            "awayTeam": "West Ham United",
            "matchStatus": " 4 : 0 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Swansea City",
            "awayTeam": "Manchester United",
            "matchStatus": " 0 : 4 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Leicester City",
            "awayTeam": "Brighton & Hove Albion",
            "matchStatus": " 2 : 0 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Burnley",
            "awayTeam": "West Bromwich Albion",
            "matchStatus": " 0 : 1 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Liverpool",
            "awayTeam": "Crystal Palace",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "AFC Bournemouth",
            "awayTeam": "Watford",
            "matchStatus": " 0 : 2 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Southampton",
            "awayTeam": "West Ham United",
            "matchStatus": " 3 : 2 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Stoke City",
            "awayTeam": "Arsenal",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 20, 2017  ",
            "homeTeam": "Huddersfield Town",
            "awayTeam": "Newcastle United",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 20, 2017  ",
            "homeTeam": "Tottenham Hotspur",
            "awayTeam": "Chelsea",
            "matchStatus": " 1 : 2 "
        },
        {
            "matchDate": "August 21, 2017  ",
            "homeTeam": "Manchester City",
            "awayTeam": "Everton",
            "matchStatus": " 1 : 1 "
        },
        {
            "matchDate": "August 26, 2017  ",
            "homeTeam": "AFC Bournemouth",
            "awayTeam": "Manchester City",
            "matchStatus": " 1 : 2 "
        },
        {
            "matchDate": "August 26, 2017  ",
            "homeTeam": "Huddersfield Town",
            "awayTeam": "Southampton",
            "matchStatus": " 0 : 0 "
        },
        {
            "matchDate": "August 26, 2017  ",
            "homeTeam": "Newcastle United",
            "awayTeam": "West Ham United",
            "matchStatus": " 3 : 0 "
        }, ...]}

您的实际输入数据是什么样子的?一个带有固定装置的页面示例数据是什么样子的:更新后,您从HTML中获取的值似乎是错误的?您将它们放入数组中,然后按索引将它们吐出-任何日期都不应更改。除非您与主客场团队的日期不是1比1,否则数组不是1对1。给我一分钟,我将整理一个解决方案。您输入的是数组还是字符串?@breakslow\u不客气!如果这是您认为最有用的解决方案,请确保将答案标记为已接受。
:)
{
    "fixtureDate": [
        {
            "matchDate": "August 11, 2017  ",
            "homeTeam": "Arsenal",
            "awayTeam": "Leicester City",
            "matchStatus": " 4 : 3 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Watford",
            "awayTeam": "Liverpool",
            "matchStatus": " 3 : 3 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Crystal Palace",
            "awayTeam": "Huddersfield Town",
            "matchStatus": " 0 : 3 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Everton",
            "awayTeam": "Stoke City",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "West Bromwich Albion",
            "awayTeam": "AFC Bournemouth",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Chelsea",
            "awayTeam": "Burnley",
            "matchStatus": " 2 : 3 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Southampton",
            "awayTeam": "Swansea City",
            "matchStatus": " 0 : 0 "
        },
        {
            "matchDate": "August 12, 2017  ",
            "homeTeam": "Brighton & Hove Albion",
            "awayTeam": "Manchester City",
            "matchStatus": " 0 : 2 "
        },
        {
            "matchDate": "August 13, 2017  ",
            "homeTeam": "Newcastle United",
            "awayTeam": "Tottenham Hotspur",
            "matchStatus": " 0 : 2 "
        },
        {
            "matchDate": "August 13, 2017  ",
            "homeTeam": "Manchester United",
            "awayTeam": "West Ham United",
            "matchStatus": " 4 : 0 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Swansea City",
            "awayTeam": "Manchester United",
            "matchStatus": " 0 : 4 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Leicester City",
            "awayTeam": "Brighton & Hove Albion",
            "matchStatus": " 2 : 0 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Burnley",
            "awayTeam": "West Bromwich Albion",
            "matchStatus": " 0 : 1 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Liverpool",
            "awayTeam": "Crystal Palace",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "AFC Bournemouth",
            "awayTeam": "Watford",
            "matchStatus": " 0 : 2 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Southampton",
            "awayTeam": "West Ham United",
            "matchStatus": " 3 : 2 "
        },
        {
            "matchDate": "August 19, 2017  ",
            "homeTeam": "Stoke City",
            "awayTeam": "Arsenal",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 20, 2017  ",
            "homeTeam": "Huddersfield Town",
            "awayTeam": "Newcastle United",
            "matchStatus": " 1 : 0 "
        },
        {
            "matchDate": "August 20, 2017  ",
            "homeTeam": "Tottenham Hotspur",
            "awayTeam": "Chelsea",
            "matchStatus": " 1 : 2 "
        },
        {
            "matchDate": "August 21, 2017  ",
            "homeTeam": "Manchester City",
            "awayTeam": "Everton",
            "matchStatus": " 1 : 1 "
        },
        {
            "matchDate": "August 26, 2017  ",
            "homeTeam": "AFC Bournemouth",
            "awayTeam": "Manchester City",
            "matchStatus": " 1 : 2 "
        },
        {
            "matchDate": "August 26, 2017  ",
            "homeTeam": "Huddersfield Town",
            "awayTeam": "Southampton",
            "matchStatus": " 0 : 0 "
        },
        {
            "matchDate": "August 26, 2017  ",
            "homeTeam": "Newcastle United",
            "awayTeam": "West Ham United",
            "matchStatus": " 3 : 0 "
        }, ...]}