Something odd is happening with my input tag in the HTML file where only half of my array elements are being processed. The input collects numbers/letters and assigns a line of code, like this:
let result = Object.fromEntries(
Object.entries(answers2).map(([k, v]) => [k, MSamples[v] ])
);
For example, if you enter "asdf", it would correspond to:
0: ['M20']
1: ['M30']
2:['M40']
3: ['M50']
length 4
So, in a for loop:
for (let i = 0; i < Object.keys(result).length; i++) { }
But strangely enough, only part of the output is being displayed:
0: ['M20']
1: ['M30']
Even when removing all other code within the for statement and just keeping a console.log('wow'), it still only logs "wow" twice.
Some people asked for a reproducible example, so here's everything you need:
var MSamples = {
"A": [
"M10",
],
"B": [
"M20",
],
"C": [
"M30",
],
"D": [
'M40',
],
}
var answers2 = document.getElementById('fname').value;
let result = Object.fromEntries(
Object.entries(answers2).map(([k, v]) => [k, MSamples[v]])
);
for (let i = 0; i < Object.keys(result).length; i++) {
console.log('wow');
}
<form class="form1 " action="">
<input type="text" id="fname" name="fname" value="ABCD">
</form>
After some investigation:
It appears that the issue arises when I include i++; at the end of my for statement. Removing all instances of i++; resolved the problem and everything worked as expected.