FOR循环和字符串与JavaScript连接给我一个未定义的值 [英] FOR loop and string concatenating with JavaScript gives me an undefined value
本文介绍了FOR循环和字符串与JavaScript连接给我一个未定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数组
var data = [name, address, city, country];
循环
var columns;
for (var i = 0; i < data.length; i++) {
columns += "data[" + i + "], ";
}
columns = columns.slice(0, -2);
alert(columns);
警告信息显示
undefineddata[0], data[1], data[2], data[3]
我在这里做错了什么?我想删除未定义的...
What am I doing wrong here? I want to remove the undefined...
推荐答案
你需要用空字符串启动,像这样:
You need to start with an empty string, like this:
var columns = "";
现在你拥有的基本相当于:
Right now what you have is basically equivalent to:
var columns = undefined;
当连接到字符串时,会给你undefined
。
Which when concatenated to a string, gives you "undefined"
.
这篇关于FOR循环和字符串与JavaScript连接给我一个未定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文