简体   繁体   中英

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?

    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:

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 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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