Good day,
I have a form where a user can select a product, the "value" stored to it is then parsed to determine the datalist name for the next drop down and the array I need to use (the array has been declared at the top of the JS)
The problem I get is when I pass the value arrayName to the function, it receives it as a string rather than the array.
in the console it shows: console.log("array:", array) --> array: washArray console.log("length:", array.length) --> length: 9
It should be: console.log("array", array) --> array: (2) [Array(3),[Array(3)] console.log("length", array.length) --> length: 2
If I change
var avgPrice = traverseArray(dlName, arrayName);
to be var avgPrice = traverseArray(dlName, washArray);
It works correctly and reads the array however arrayName shows its equal to washArray ... however its the string not the array,
How do I make it read the array with name washArray instead of a string.
<div id="AvgCalcWrap" class="hidden">
<form id="AvgCalc" class="table-bordered mod-data-default-template jobsdata generaltable boxaligncenter">
<div>
<label class="large-label"><b>Product: </b><span class="small-label"></span><br></label>
<select name="dlproduct" id="dlproduct" onchange="myFunction()">
<option id="0" value="" selected="" disabled="">« Please Select a Product »</option>
<option id="1" value='["refModels", "refArray"]'>Refrigerator</option>
<option id="2" value='["washModels", "washArray"]'>Washing Machine</option>
</select><br>
</div>
</form>
const washArray = [
["F3S1CWK2EV","101","2726.76"],
["F3S1CWK2EV","107","1919.23"]
]
function avgCalc() {
var values = document.getElementById("dlproduct");
var val = values.options[values.selectedIndex].value;
var array = JSON.parse(val);
var dlName = array[0];
var arrayName = array[1]; <--- *Issue here*
console.log("dlname", dlname);
console.log("arrayName", arrayName);
var avgPrice = traverseArray(dlName, arrayName);}
function traverseArray(dl, array){
console.log("Array:", array); <--- *Issue here*
console.log("length", array.length)} <--- *Issue here*