我试着逐行打印字符串。

时间:2014-05-03 07:09:23

标签: javascript

var animal = new Array("s","b","c","k","h","g"); 
var animalString =''; 
for(var m=0; m<animal.length; m++){ 
    //console.log(m)
    animalString+= animal[m] +"\n";
    console.log(animalString);
}

4 个答案:

答案 0 :(得分:0)

您需要做的就是:

var animal = ["s","b","c","k","h","g"];
for (var x = 0; x < animal.length; x++) {
    console.log(animal[x]);
}

没有必要尝试将其放入字符串并将其打印到控制台。无论如何,console.log()在控制台内都有回车符。

答案 1 :(得分:0)

只是建议使用加入

var animal = new Array("s","b","c","k","h","g"); 
var animalString = animal.join("\n");

    //console.log(m)
    console.log(animalString);

<强> Demo

答案 2 :(得分:0)

不确定,但我认为你正在寻找这个:

 var animal = new Array("s","b","c","k","h","g"); 
 var animalString = animal.join().replace(/\,/, '<br/>');

答案 3 :(得分:0)

试试这个:

var animal = new Array("s","b","c","k","h","g"); 
var animalString =''; 
for(var m=0; m<animal.length; m++){  
    console.log(animal[m]);
}

Working Fiddle

您不需要任何字符串操作或在此使用+=,因为animal是一个数组对象,正如您所说,您只想要字符串like s b c k h in line by line。 因此,for循环逐个执行所有数组项。