Entonces, como dice el título, mi booleano no se evalúa correctamente, aquí está mi código:
var unindexed_frmVersionCtrl = $("#frmVersionCtrl").serializeArray(); unindexed_frmVersionCtrl[unindexed_frmVersionCtrl.length] = { name: "versionControl", value: 0 }; console.log(unindexed_frmVersionCtrl); let historyEntry = false; for (const [key, value] of Object.entries(unindexed_frmVersionCtrl)) { if (value.name == "versionControlBool") { historyEntry = value.value; console.log(historyEntry); } if (value.name == "versionControl") { console.log(historyEntry); if (historyEntry == true) { value.value = 1; console.log("lel"); } else { console.log("false"); value.value = 0; } } } console.log(unindexed_frmVersionCtrl);aquí está la salida:
historyEntry se evalúa como verdadero incluso cuando es falso. No se que hacer. Ni idea. Estoy empezando con javascript y nunca he estado tan confundido. Gracias.
EDITAR: error tipográfico.
El problema es su historyEntry = value.value; , en este caso value.value es una cadena, no un bool.
Puedes hacer historyEntry = (value.value == 'true');
Manifestación
var unindexed_frmVersionCtrl = [{ "name": 'versionControlBool', value: 'true' }, { "name": 'versionCount', value: '25' }, { "name": 'versionControl', value: 0 }] var historyEntry = false; for (const [key, value] of Object.entries(unindexed_frmVersionCtrl)) { if (value.name == "versionControlBool") { historyEntry = (value.value == 'true'); console.log("typeof value.value = " + typeof(value.value)) console.log(historyEntry); } if (value.name == "versionControl") { console.log(historyEntry); if (historyEntry == true) { value.value = 1; console.log("lel"); } else { console.log("false"); value.value = 0; } } } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>El valor de 'versionControlBool' es una cadena 'true' no boolean true . Si intenta 'true' == true obtendrá false pero si console.log('true') la consola registrará true como si fuera un valor booleano para que no se dé cuenta. Aquí hay un código para probarlo.
var x = true; var y = 'true'; console.log(x, 'This is the boolean true'); console.log(y, 'This is a string "true"'); console.log(x == true, 'This should be true'); console.log(y == true, 'This will be false even though the console thinks it\'sa boolean'); console.log(y == 'true', 'Whereas this will be true, as it should');