Home > Enterprise >  Array manipulation with JavaScript
Array manipulation with JavaScript

Time:10-11

I have an Array and I want to check the data in it with an if block and mark it if it exists in the array.

doc.setFontSize(9);
doc.text("Açıklama",6,57);
doc.text("Hastaya Yapılan Uygulama :",6,64);
doc.text("Kullanılan İlaçlar",6,69);
var explanation_application = document.getElementById("explanation_application").value;
textlines = doc.setFontSize(9).splitTextToSize(explanation_application,90);
doc.text(textlines,48,57).value;

doc.setFontSize(8);
doc.text("İzolasyon Durumu:",136,66);

doc.setFontSize(8);
doc.text("Solunum İzolasyonu",163,60);
var checkBox = new jspdf.AcroFormCheckBox();
var checkBoxTxt = document.getElementById("txt").value;
var splitTxt = checkBoxTxt.split(",");
for (let i = 0; i < splitTxt.length; i  ){
// for (var state in splitTxt){
    if(splitTxt[i] == 'solunum_izolasyonu') {
       
        checkBox.appearanceState = 'On';
    }
    else {
        
        checkBox.appearanceState = 'Off';
    }
}
checkBox.readOnly = false;
checkBox.fieldName = "Solunum İzolasyonu";
checkBox.Rect = [191, 58, 2, 2];
checkBox.value = 'solunum_izolasyonu';
doc.addField(checkBox);

doc.setFontSize(8);
doc.text("Damlacık İzolasyonu",163,66);
var checkBox1 = new jspdf.AcroFormCheckBox();
var checkBoxTxt1 = document.getElementById("txt").value;
var splitTxt1 = checkBoxTxt1.split(",");
for (let i = 0; i < splitTxt1.length; i  ){
    // for (var state in splitTxt){
    if(splitTxt1[i] == 'damlacik_izolasyonu') {
        
        checkBox1.appearanceState = 'On';
    }
    else {
        
        checkBox1.appearanceState = 'Off';
    }
}
checkBox1.readOnly = false;
checkBox1.fieldName = "Damlacık İzolasyonu";
checkBox1.Rect = [191, 64, 2, 2];
checkBox.value = 'damlacik_izolasyonu';
doc.addField(checkBox1);

doc.setFontSize(8);
doc.text("Temas İzolasyonu",163,72);
var checkBox2 = new jspdf.AcroFormCheckBox();
var checkBoxTxt2 = document.getElementById("txt").value;
var splitTxt2 = checkBoxTxt2.split(",");
for (let i = 0; i< splitTxt2.length; i  ){
    // for (var state in splitTxt){
    if(splitTxt2[i] == 'temas_izolasyonu') {
        
        checkBox2.appearanceState = 'On';
    }
    else {
        
        checkBox2.appearanceState = 'Off';
    }
}
checkBox2.readOnly = false;
checkBox2.fieldName = "Temas İzolasyonu";
checkBox2.Rect = [191, 70, 2, 2];
checkBox.value = 'temas_izolasyonu';
doc.addField(checkBox2);

When I run my code like above, even though there are 2 data in the array, only 1 is marked.I'm new to this field, and what I want to do here is to pull the ids of the checkboxes marked on a form and display it on a pdf. Here I am doing this process using the jsPdf module, but after reaching this stage, I had a problem with the marking point, I would be glad if you could help with this.

CodePudding user response:

You are looping over the array and resetting it if it has a value. To make your code work it would need to look something like

let hasText = false;
for (let i = 0; i< splitTxt2.length; i  ){
    if(splitTxt2[i] == 'temas_izolasyonu') {
        hasText = true;
        // have a match, no need to keep looping so exit
        break;
    }
}
checkBox.appearanceState = hasText ? 'On' : 'Off';

And the cleanest solution is includes

const hasText = splitTxt2.includes('solunum_izolasyonu');
checkBox.appearanceState = hasText ? 'On' : 'Off';
  • Related