I have a table of inputs with the headers: inside dimension, outside dimension, and quantity. A user can input up to 20 different instances. For example user inputs:
Line 1 - ID = 20, OD = 30, QTY = 6
Line 2 - ID = 10, OD = 15, QTY = 3
Line 3 - ID = 40, OD = 52, QTY = 15
etc...
Currently I am collecting all of the ID's, OD's, and QTY's in parallel arrays, so for this example:
IDarray = [20, 10, 40, etc...]
ODarray = [30, 15, 52, etc...]
QTYarray = [6, 3, 15, etc...]
I have a loop that goes through the table and ignores any blank inputs when building those arrays. All of that is working fine, but I want to be able to sort them from largest to smallest based on the outside diameter (OD). It looks like the best way to do so would be to combine the 3 parallel arrays into an array of objects that contains all three elements, but I can't seem to find guidance for building an array of objects when the input values can constantly be different. I am sure this is very basic, but I am new to coding javascript, so I am struggling with this.
I have attached a snippet of my code, but I reduced it to 3 inputs to save space.
<script>
window.onload = function() {
$("#unit1").val(default_unit);
$("#unit2").val(default_unit);
$("#unit3").val(default_unit);
function myFunction() {
var unit1 = document.getElementById("unit1").value;
var unit2 = document.getElementById("unit2").value;
var unit3 = document.getElementById("unit3").value;
var id1 = (unit1 == 1) ? document.getElementById("id1").value:document.getElementById("id1").value / 25.4;
var id2 = (unit2 == 1) ? document.getElementById("id2").value:document.getElementById("id2").value / 25.4;
var id3 = (unit3 == 1) ? document.getElementById("id3").value:document.getElementById("id3").value / 25.4;
var od1 = (unit1 == 1) ? document.getElementById("od1").value:document.getElementById("od1").value / 25.4;
var od2 = (unit2 == 1) ? document.getElementById("od2").value:document.getElementById("od2").value / 25.4;
var od3 = (unit3 == 1) ? document.getElementById("od3").value:document.getElementById("od3").value / 25.4;
var qty1 = document.getElementById("qty1").value;
var qty2 = document.getElementById("qty2").value;
var qty3 = document.getElementById("qty3").value;
Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};
var id_values = [id1, id2, id3].clean("");
var od_values = [od1, od2, od3].clean("");
var qty_values = [qty1, qty2, qty3].clean("");
alert(id_values);
alert(od_values);
alert(qty_values);
//var testObject = {
// id_values,
// od_values, <--sort by this
// qty_values;
//};
}
</script>
<table class="inputstable">
<tr>
<th>Unit</th>
<th>Inside Dimension</th>
<th>Outside Dimension</th>
<th>Quantity</th>
</tr>
<tr>
<td><select class="custom_select" id="unit1">
<option value=1>in</option>
<option value=2>mm</option>
</td>
<td><input class="custom_input" type="text" id="id1"></td>
<td><input class="custom_input" type="text" id="od1"></td>
<td><input class="custom_input" type="number" id="qty1"></td>
</tr>
<tr>
<td><select class="custom_select" id="unit2">
<option value=1>in</option>
<option value=2>mm</option>
</td>
<td><input class="custom_input" type="text" id="id2"></td>
<td><input class="custom_input" type="text" id="od2"></td>
<td><input class="custom_input" type="number" id="qty2"></td>
</tr>
<tr>
<td><select class="custom_select" id="unit3">
<option value=1>in</option>
<option value=2>mm</option>
</td>
<td><input class="custom_input" type="text" id="id3"></td>
<td><input class="custom_input" type="text" id="od3"></td>
<td><input class="custom_input" type="number" id="qty3"></td>
</tr>
<button class="special_button" onclick="myFunction()">Tester</button>
Thanks!