简体   繁体   English

如何摆脱第一个未定义

[英]How to get rid of the first undefined

Is there no easier way to get rid of the first element of undefined than this, by checking in the loops it isn't the first element? 通过检查循环中不是第一个元素的方法,没有比这更容易的方法来摆脱undefined的第一个元素了吗?

    var text;

    for (d=0; d<json_nya_svar.length; d++){
        for (c=0; c<json_nya_svar[d].length; c++) {
            if (c==0 && d==0) text=json_nya_svar[d][c] + ";";
            else text += json_nya_svar[d][c] + ";";}
    text += "\r\n";
    }

Just give text an initial value: 只需给text一个初始值即可:

var text = '';

Also, bear in mind that your loop variables are global, which can lead to various problems in the long run. 另外,请记住,循环变量是全局变量,从长远来看,这可能导致各种问题。 Don't forget to add var before their declaration: 不要忘记在声明之前添加var

var text = '';
var json_nya_svar = [[1, 2, 3], [4, 5, 6]];

for (var d=0; d<json_nya_svar.length; d++){
    for (var c=0; c<json_nya_svar[d].length; c++) {
        text += json_nya_svar[d][c] + ";";
    }
    text += "\r\n";
}

console.log(text);

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

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