Hint: What do you do if SelectedRadiator is undefined?
(More than likely, youâve managed to create an array of dropdown choices that corresponds to NONE of your radiators. Imagine what happens if your dropdowns are set to 3, 200, 50.)
No, that is not the issue. I created code to make sure that the drop-downs can only select actually existing radiators from the table. The real table is much longer.
You will get that error if find() does not find an element in the array that satisfies the testing function WritePrice().
In other words make sure the three dropdown values selected are valid for one of the array elements. So for example 2, 300, 30 would be be OK; but 2, 300, 50 would give that error.
You could check whether SelectedRadiator is undefined:
if(SelectedRadiator) document.getElementById("Price").innerHTML = SelectedRadiator[4]
else document.getElementById("Price").innerHTML = "No radiator found";
But itâs probably better to arrange the dropdowns so only valid options can be selected.
One option is to maintain the list of radiators using a spreadsheet: for example using LibreOffice Calc.Then export the data to a CSV format file. After uploading that file to your server, use JavaScript to read the file using fetch() and use split() to create the multi-dimensional array or JavaScript object.
Personally I would add a JavaScript comment to describe the array items rather than use an object.
LibreOffice Calc does not export/save JSON. I donât know about Microsoft Excel.
Thatâs certainly an option but you can put what you want in a comment .
Itâs personal choice. I donât see that you really gain much by using an array of objects instead of an array of arrays. I guess processing time is quicker with arrays but itâs very unlikely to be significant.
Not my area of expertise but I tested an online CSV to JSON converter and it seems to do a decent job. Which is why I did ask the question.
I confess I am suffering with a bit of brain fog right now, but readability? Surely being able to access the data by key names, âwidthâ, âheightâ etc rather than working with index numbers makes sense. Using comments , just doesnât seem best practice to me, but maybe I am over complicating things.
And then someone inserts a column in the excel file and all your code does no longer workâŚ.
I hate such static things relying on fix excel sheet.
I would go much further and not even read a csv but the excel itself and check the column headers values. Yes, this is much more work, but you can never ever trust on a standard excel user to understand the need of not changing column orders, not enter second title row etc.
I know this from many years of experience on importing excel data âŚ
Thatâs fair comment. Your approach would cope with changes to the spreadsheet columns provided the text heading each column remains as expected. Both approaches would need to check whether the data appears valid and if necessary give an error message to website visitors.
For my approach a simple initial check would to be to ensure the first line of the CSV file starts with âHeight,Width,Blades,Type,Priceâ. A second title row would not be an issue.
To find a specific value in a multidimensional array, you can iterate through its nested arrays using loops. For example, in JavaScript:
const multidimensionalArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
const targetValue = 5;
let found = false;
for (let i = 0; i < multidimensionalArray.length; i++) {
for (let j = 0; j < multidimensionalArray[i].length; j++) {
if (multidimensionalArray[i][j] === targetValue) {
found = true;
break;
}
}
if (found) {
break;
}
}
if (found) {
console.log(Value ${targetValue} found in the array.);
} else {
console.log(Value ${targetValue} not found in the array.);
}