'how to structure objects within an array while scraping
I'm trying to structure this array to contain data for one golf hole in one object within the array. Example of what I'm trying to achieve.
Ex.
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
},
]
The issue is how I am collecting this data. Each data collection could either have 9 holes or 18 holes and will have multiple tee colors, ex. blue, white, red on one course. Just blue and white on another.
Example of scorecard. Each card is a separate table and each row is a with cells being within row. scorecard
Here is what I have so far but I'm not good at structuring data within these complex arrays.
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;
}),
);
Output.
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',
'', ''
]
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
