I am writing code in javaScript to display a table containing JSON array object data. However, I need to verify that the data in one particular object matches the next object. For example, I have listed my code below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table Example</title>
</head>
<body>
<div id="table-container"></div>
<script type="module">
import finnhub from 'https://cdn.skypack.dev/finnhub'
const api_key = finnhub.ApiClient.instance.authentications['api_key'];
api_key.apiKey = "cp3lm91r01qs3665m6p0cp3lm91r01qs3665m6pg"
const finnhubClient = new finnhub.DefaultApi()
finnhubClient.financialsReported({"symbol": "TSLA"}, (error, data, response) => {
console.log(data)
let d = 0
let b = 0
let s = []
let h = 0
window.d1 = data.data.length
window.b1 = data.data[b].report.bs.length
for (let d = 0; d < window.d1; d = d + 1) {
console.log("d = " + d)
//console.log("Data.data.length = " + data.data.length)
window.b1 = data.data[d].report.bs.length
for (let b = 0; b < window.b1; b = b + 1) {
console.log("b = " + b)
//console.log(data.data[d].report.bs[b].label + " = " + data.data[d].report.bs[b].value)
// The code below as is lists matching '.label' parameters, but presumably encounters an error when there are more data.data.report.bs values in 'd' than there are in 'd+1'
// I need to code a way to prevent the second part of the if statement's expression from checking for a data.data value that does not exist as I am reading an Uncaught Typeerror:Cannot read...undefined
if (data.data[d].report.bs[b].label = data.data[d+1].report.bs[b].label){
console.log(data.data[d].report.bs[b].label)
console.log(data.data[d+1].report.bs[b].label)
} else {
console.log("Algorithm mismatch")
}
}
}
// Function to create a table
function createTable(rows, cols) {
// Create a table element
let table = document.createElement('table');
table.border = '1';
// Loop through rows
for (let i = 0; i < rows; i++) {
// Create a table row
let tr = document.createElement('tr');
// Loop through columns
for (let j = 0; j < cols; j++) {
// Create a table cell
if (j == 0){
let td = document.createElement('td');
td.textContent = data.data[0].report.bs[i].label;
td.style = "width:300px"
tr.appendChild(td);
} else if (j == 1){
let td = document.createElement('td');
td.textContent = data.data[0].report.bs[i].value;
td.style = "width:150px"
tr.appendChild(td);
} else if (j == 2){
let td = document.createElement('td');
td.textContent = ``;
td.style = "width:25px"
tr.appendChild(td);
}
}
// Append the row to the table
table.appendChild(tr);
}
// Append the table to the container
document.getElementById('table-container').appendChild(table);
}
// Call the function to create a table with 5 rows and 3 columns
createTable(data.data[0].report.bs.length, 3);
})
;
</script>
</body>
</html>
My problem occurs within the if statement/loop. Whereupon the second part of the if statement’s expression (… = data.data[d+1].report.bs[b].label). Specifically, as glancing at the JSON of console log of the data will show, some of the balance-sheet(“bs”) JSON hierarchy contain less array values than others; which means that not all of the labels will match up entirely.
To help explain, if you glance at the table I have constructed, you will see white boxes next to the number values of the financial data. I am planning on creating color coded fillings for the boxes to gauge their prospect value. However, to do that, I need to match the …bs[b].label values to ensure that they are the same description.
I apologize if I have not been 100% clear on this and will be more than happy to explain further.
I have tried a number of loops including while loops, for loops, etc. I expected to create a solution that stepped step-by-step to systematically reduce the number of times the second expression in the IF statement is checked in order to prevent the error code of ‘Cannot Read Undefined’. What resulted was a mixture of program breaks and error codes all conveying that either the data was not defined or that the loop structure was not completely finished.