简体   繁体   English

在for循环中以undefined开头的字符串变量

[英]String variable prefixed with undefined in for loop

I have a drop-down on which i use .Change() to trigger a function. 我在下拉菜单中使用.Change()触发函数。 Function basically get certain data using getJSON and based on those value in have to create string of array for mp3 file. 函数基本上使用getJSON获取某些数据,并基于这些值必须为mp3文件创建数组字符串。

Below code is generating string but always prefix undefined to string. 下面的代码生成字符串,但始终undefined前缀为字符串。

In code you will notice setTimeout which is just to provide certain delay till data received. 在代码中,您会注意到setTimeout ,它只是提供一定的延迟直到接收到数据。 In below example i am using static value and it still prefix undefined . 在下面的示例中,我使用的是静态值,它仍然以undefined前缀。 not sure why may be i have defined variable in wrong manner. 不知道为什么我可能以错误的方式定义了变量。

Complete example JSBin 完整的示例JSBin

$('.customSurah').change(function(){
    //surahNo = $('#surah option:selected').val();

    setTimeout(function(){ 
    //countSpan = $('#surah-wrapper').children().length;
    surahNo = 1;
    countSpan = 7;
    var i=0;
         for (i = 0; i <= countSpan; i++) {
            strCat += surahNo+"/"+i+".mp3,";
            console.log(strCat);
        }
    }, 3000);

 });

OUTPUT 输出值

undefined114/0.mp3,
undefined114/0.mp3,114/1.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,114/3.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,114/3.mp3,114/4.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,114/3.mp3,114/4.mp3,114/5.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,114/3.mp3,114/4.mp3,114/5.mp3,114/6.mp3,

You have a variable strCat that is not initialized, and then you append a value to it in this line: 您有一个未初始化的变量strCat ,然后在此行中向其附加一个值:

strCat += surahNo+"/"+i+".mp3,";

Since strCat is not initialized in first round of loop, you get undefined prepended to your string. 由于strCat不在循环的第一轮中初始化,因此您的字符串之前undefined

To fix this, you need to initialize the variable to empty value first: 要解决此问题,您需要先将变量初始化为空值:

var strCat = '';   // <- initialize your variable to empty value
surahNo = 1;
countSpan = 7;

The outcome is perfectly valid as per javascript is concerned. 根据javascript而言,结果是完全有效的。

Why? 为什么?

I guess you probably know if you declare any variable in javascript and you don't assign its default value then automatically undefined is assigned. 我想您可能知道,如果您在javascript中声明了任何变量,并且未分配默认值,则会自动分配undefined So, that is a valid. 因此,这是有效的。 What happens when you do that: 当您这样做时会发生什么:

 var somevar; // non assigned default value set to -> undefined console.log(somevar); // logs undefined 

But, 但,
In your case you have to give it a default value like a blank string var strCat ""; 在您的情况下,您必须给它一个默认值,例如空白字符串var strCat ""; . So, now when you do this: 因此,现在当您执行此操作时:

 var somevar = ""; // assigned default value to set to -> "" console.log(somevar); // logs "" 


So, the solution to your issue is, you have to initialize/assign a default value to your variable. 因此,解决此问题的方法是,您必须初始化/将默认值分配给变量。 like: 喜欢:

var strCat = "";

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

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