I am taking an old hardcoded website of mine and trying to strip the data out of the HTML and drop it into a new JSON object.
Currently I am receiving a table of items (reduced for simplicity) as 1 giant string, there are almost 1000 rows. There are no classes or attributes on any of the HTML
let tableString = `
<tr>
<td>01/01/1999</td>
<td>Item 1</td>
<td>55</td>
</tr>
<tr>
<td>01/01/2000</td>
<td>Item 2</td>
<td>35</td>
</tr>
`
I am working towards achieving the following object
[{
date: '01/01/1999',
name: 'Item 1',
cost: 55
},
{
date: '01/01/2000',
name: 'Item 2',
cost: 35
}]
Current code I have implemented
let newData = []
let stringArray = results.split('</tr>')
stringArray.map(item => {
let stripped = item.replace('/n', '')
stripped = stripped.replace('<tr>', '')
let items = stripped.split('<td>')
let newItem = {
data: items[0],
name: items[1],
cost: items[2]
}
return newData.push(newItem)
})
I am taking the giant string and splitting it at the end of every item. This works however it strips the actual tag out of the item itself and leaves me with an extra (empty string item in my array).
Next I am mapping over each string in my array and further trying to strip all line breaks out as well as the in order to have an array of table cells, then In theory I can build out my object (after I strip the table cells out).
However as I am doing this replace doesnt seem to be working, is my thinking process correct on how I am moving forward, should I look at regex patterns to target this better?