1

この関数が壊れる理由がわかりましたか?

window を使用してグローバル配列の値を取得します。console.log で arr オブジェクトの値を取得しますが、foreach で arr オブジェクトを使用しようとすると、コードが壊れます。理由はありますか?

function setDropDownList(raw_id, val){
    // Get the ID
    var the_id = '#'+raw_id;
    // Get the array name as a string
    var arrname = val+"Array";
    // get the arr object using the arrname string
    var arr = window[arrname];
    // if I place a console.log here, i get the values for the arr object, but it brakes the .each below:

    // do a simple foreach
    // using arr breaks the foreach loop
    // if I use the actual array declared globally in he header it works.
    jQuery.each(arr, function(key, value) {
        var test = value.split('|');
    });
}   
4

1 に答える 1

1

これは、配列内の項目の 1 つ (またはすべて) が文字列ではないためです。

を行う場合value.split('|')、値は文字列である必要があります。そうしないと、壊れます。

于 2013-03-01T18:53:36.967 に答える