如何在抓取时构建数组中的对象

how to structure objects within an array while scraping

我试图构造此数组以包含数组中一个对象中一个高尔夫球洞的数据。我正在努力实现的示例。

例如

             let scorecard = [
                {
                    hole: 1,
                    par: 4,
                    blue: 375,
                    white: 300,
                    red: 250,
                    handicap: 1
                },
                {
                    hole: 2,
                    par: 3,
                    blue: 175,
                    white: 150,
                    red: 125,
                    handicap: 2
                },
            ]

问题是我如何收集这些数据。每个数据集合可能有 9 个洞或 18 个洞,并且会有多种发球台颜色,例如。一道菜上有蓝色、白色、红色。另一个只是蓝色和白色。

记分卡示例。每张卡片都是一个单独的 table,每一行都是一个单元格在行内。 scorecard

这是我目前所掌握的,但我不擅长在这些复杂数组中构建数据。

            const res = await page.$$eval("table > tbody > tr", (holeInfo) =>
            holeInfo.map((hole) => {
                let dataStructure = []
                let dataObj = {}
                let scoreInfo = hole.querySelector("td").innerText
                let holeInfo = hole.querySelectorAll("td")

                holeInfo.forEach((x) => {
                    dataStructure.push(
                        x.innerHTML
                    )
                })

                return dataStructure;
            }),
        );

输出。

res [
[
    'Hole:', '1',   '2',
    '3',     '4',   '5',
    '6',     '7',   '8',
    '9',     'Out'
  ],
  [
    'Par:', '4',  '4',
    '4',    '4',  '4',
    '3',    '4',  '3',
    '5',    '35'
  ],
  [
    'Blue:', '372',
    '394',   '369',
    '361',   '317',
    '188',   '342',
    '184',   '570',
    '3097'
  ],
  [
    'Blue:', '372',
    '394',   '369',
    '361',   '297',
    '188',   '342',
    '184',   '570',
    '3077'
  ],
  [
    'White:', '330',
    '389',    '364',
    '291',    '285',
    '181',    '290',
    '166',    '549',
    '2845'
  ],
  [
    'White:', '0',
    '0',      '0',
    '0',      '0',
    '0',      '0',
    '0',      '0',
    '0'
  ],
  [
    'Red Intermediate:',
    '301',
    '370',
    '248',
    '275',
    '260',
    '167',
    '245',
    '99',
    '452',
    '2417'
  ],
  [
    'gold:', '0', '0',
    '0',     '0', '0',
    '0',     '0', '0',
    '0',     '0'
  ],
  [
    'Handicap:', '10',
    '4',         '5',
    '18',        '16',
    '13',        '12',
    '17',        '1',
    ''
  ],
  [
    'Hole:', '10', '11',
    '12',    '13', '14',
    '15',    '16', '17',
    '18',    'In', 'Total'
  ],
  [
    'Par:', '4',  '5',
    '4',    '4',  '4',
    '3',    '4',  '4',
    '4',    '35', '71'
  ],
  [
    'Blue:', '367',  '481',
    '352',   '313',  '323',
    '186',   '439',  '379',
    '318',   '3158', '6255'
  ],
  [
    'Blue:', '367',  '481',
    '352',   '313',  '323',
    '186',   '439',  '379',
    '318',   '3158', '6235'
  ],
  [
    'White:', '337',
    '467',    '337',
    '302',    '315',
    '14',     '429',
    '349',    '280',
    '2990',   '5835'
  ],
  [
    'White:', '0',
    '0',      '0',
    '0',      '0',
    '0',      '0',
    '0',      '0',
    '0',      '5835'
  ],
  [
    'Red Intermediate:',
    '303',
    '443',
    '311',
    '264',
    '299',
    '156',
    '278',
    '333',
    '255',
    '2642',
    '5059'
  ],
  [
    'gold:', '0', '0',
    '0',     '0', '0',
    '0',     '0', '0',
    '0',     '0', '0'
  ],
  [
    'Handicap:', '7',
    '3',         '11',
    '15',        '14',
    '6',         '2',
    '9',         '8',
    '',          ''
  ]
]

let res = [
[
    'Hole:', '1',   '2',
    '3',     '4',   '5',
    '6',     '7',   '8',
    '9',     'Out'
  ],
  [
    'Par:', '4',  '4',
    '4',    '4',  '4',
    '3',    '4',  '3',
    '5',    '35'
  ],
  [
    'Blue:', '372',
    '394',   '369',
    '361',   '317',
    '188',   '342',
    '184',   '570',
    '3097'
  ],
  [
    'Blue:', '372',
    '394',   '369',
    '361',   '297',
    '188',   '342',
    '184',   '570',
    '3077'
  ],
  [
    'White:', '330',
    '389',    '364',
    '291',    '285',
    '181',    '290',
    '166',    '549',
    '2845'
  ],
  [
    'White:', '0',
    '0',      '0',
    '0',      '0',
    '0',      '0',
    '0',      '0',
    '0'
  ],
  [
    'Red Intermediate:',
    '301',
    '370',
    '248',
    '275',
    '260',
    '167',
    '245',
    '99',
    '452',
    '2417'
  ],
  [
    'gold:', '0', '0',
    '0',     '0', '0',
    '0',     '0', '0',
    '0',     '0'
  ],
  [
    'Handicap:', '10',
    '4',         '5',
    '18',        '16',
    '13',        '12',
    '17',        '1',
    ''
  ],
  [
    'Hole:', '10', '11',
    '12',    '13', '14',
    '15',    '16', '17',
    '18',    'In', 'Total'
  ],
  [
    'Par:', '4',  '5',
    '4',    '4',  '4',
    '3',    '4',  '4',
    '4',    '35', '71'
  ],
  [
    'Blue:', '367',  '481',
    '352',   '313',  '323',
    '186',   '439',  '379',
    '318',   '3158', '6255'
  ],
  [
    'Blue:', '367',  '481',
    '352',   '313',  '323',
    '186',   '439',  '379',
    '318',   '3158', '6235'
  ],
  [
    'White:', '337',
    '467',    '337',
    '302',    '315',
    '14',     '429',
    '349',    '280',
    '2990',   '5835'
  ],
  [
    'White:', '0',
    '0',      '0',
    '0',      '0',
    '0',      '0',
    '0',      '0',
    '0',      '5835'
  ],
  [
    'Red Intermediate:',
    '303',
    '443',
    '311',
    '264',
    '299',
    '156',
    '278',
    '333',
    '255',
    '2642',
    '5059'
  ],
  [
    'gold:', '0', '0',
    '0',     '0', '0',
    '0',     '0', '0',
    '0',     '0', '0'
  ],
  [
    'Handicap:', '7',
    '3',         '11',
    '15',        '14',
    '6',         '2',
    '9',         '8',
    '',          ''
  ]
]

function getHolesAmount(arr) {
  let holesAmount = 0;
  for (let i = 1; i < arr.length; i++) {
    if (parseInt(res[0][i])) {
      holesAmount += 1;
    } else {
      break;
    };
  };
  return holesAmount;
};

function fillScorecard(holesAmount, jStart, jEnd) {
  for (let i = 1; i <= holesAmount; i++) {
    let holeData = {};
    for (let j = jStart; j < jEnd; j++) {
      // remove ":" from the key and get value as integer
      holeData[res[j][0].slice(0, -1)] = parseInt(res[j][i]);
    };
    scorecard.push(holeData);
  };
};

// get first holes amount
let firstHolesAmount = getHolesAmount(res[0]);

// get second holes starting index
let secondHolesStart = 0;
for (let i = 1; i < res.length; i++) {
  if (res[i][0] === res[0][0]) {
    secondHolesStart = i;
    break;
  };
};
// get second holes amount
let secondHolesAmount = getHolesAmount(res[secondHolesStart]);

let scorecard = [];
// fill scorecard with first holes data
fillScorecard(firstHolesAmount, 0, secondHolesStart);
// fill scorecard with second holes data
fillScorecard(secondHolesAmount, secondHolesStart, res.length);

console.log(scorecard);