如何避免在字符串连接中未定义? [英] How to avoid undefined in string concatenation?

查看:34
本文介绍了如何避免在字符串连接中未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历一个对象,并且想要连接服务的名称.这是我的代码:

I'm iterating over an object and I want to concatenate the name of the service. This is my code:

var servizi;

for(var i = 0; i < appointment.id_services.length; i++)
{
    servizi += appointment.id_services[i].name + " ";
}

现在的问题是我得到了这个结果:

Now the problem's that I got this result:

未定义的发型

在我的对象中只有头发和伤口,为什么我也无法定义?

In my object there's only hair and cut, why I get undefined also?

推荐答案

由于声明了未初始化的变量,然后对其进行了两次添加,因此您未定义.

You get undefined because you declared an uninitialized variable, and then added to it (twice).

首先将声明的变量初始化为空字符串

Initialize the declared variable as an empty string first

var servizi = "";

这篇关于如何避免在字符串连接中未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆