简体   繁体   English

JS:根据条件在for循环内分配变量值

[英]JS: Assign variable value inside for loop depending on condition

I need to add 我需要添加

if (obj1[i][0] !== d1 && row > -1) continue;

and

if (obj1[i][1] !== d2 && row > -1) continue;

depending on the value of d1 and d2 but I'm not sure what I'm doing wrong. 取决于d1 and d2的值,但我不确定自己在做什么错。

if statement acts as a filter it will allow only those values which assigned to d1 or d2 eg if d1=1 it will print only values with 1 and not all values in the table. if语句用作过滤器,则将仅允许分配给d1d2那些值,例如,如果d1=1 ,则仅打印表中带有1的值,而不是所有值。

$("#session_id, #start_date").on('change', function() {
    var d1 = $( "#session_id" ).val();
    var d2 = $( "#start_date" ).val();

    $("#tbody").empty();


    if(d1 != null || d1 != undefined){
        kl1 = "if (obj1[i][0] !== d1 && row > -1) continue;";
        v(kl1);
    }

    if(d2 != null || d2 != undefined){
        var kl = "if (obj1[i][1] !== d2 && row > -1) continue;"
        v(kl);
    }

    function v(data){
        for (var i = 0; i < obj1.length; i++) {
                data;
            row++;
        var newTr = table.insertRow(-1);
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][1]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][2]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][3]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][4]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][5]));
    }
}
});

I think, this is what you want. 我想,这就是您想要的。 The way you're thinking to pass statement to loop actually doesn't work. 您正在考虑将statement传递给loop实际上不起作用。

We can simplify the following code 我们可以简化以下代码

d1 != null || d1 != undefined

by using javascript truthy & falsey 通过使用JavaScript 正确与错误

See the code below: 请参见下面的代码:

$("#session_id, #start_date").on('change', function() {
    var d1 = $( "#session_id" ).val();
    var d2 = $( "#start_date" ).val();

    $("#tbody").empty();

    for (var i = 0; i < obj1.length; i++) {
        if (d1 && obj1[i][0] !== d1 && row > -1) continue;
        if (d2 && obj1[i][1] !== d2 && row > -1) continue;
        row++;
        var newTr = table.insertRow(-1);
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][1]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][2]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][3]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][4]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][5]));
    }
});

Just remove statements out of the values and convert it to Boolean and pass it to the function 只需从值中删除语句,然后将其转换为Boolean值并将其传递给function

if(d1 != null || d1 != undefined){
        kl1 = obj1[i][0] !== d1 && row > -1
        v(kl1);
    }

    if(d2 != null || d2 != undefined){
        var kl = obj1[i][1] !== d2 && row > -1
        v(kl);
    }

    function v(data){
        for (var i = 0; i < obj1.length; i++) {
                if(data) continue;
            row++;
            var newTr = table.insertRow(-1);
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][1]));
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][2]));
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][3]));
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][4]));
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][5]));
        }
    }

The assignment that you are giving for k1 is string. 您为k1分配的分配是字符串。 Thats why instead of passing continue it is passing the whole statement. 这就是为什么不传递继续而是传递整个语句的原因。 Please find below the way you can do. 请在下面找到您可以采取的方法。

if(d1 != null || d1 != undefined){
    if (obj1[i][0] !== d1 && row > -1) kl = "true";
    v(kl);
}

if(d2 != null || d2 != undefined){
    if (obj1[i][1] !== d2 && row > -1) kl =  "true"
    v(kl);
}

function v(data){
    for (var i = 0; i < obj1.length; i++) {
            if(data=="true") continue
        row++;
    var newTr = table.insertRow(-1);
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][1]));
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][2]));
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][3]));
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][4]));
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][5]));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM