0

source:オプションのgeonames.org都市でオートコンプリートを使用していて、結果の配列の一部を操作したいと考えています。「ハードコードされた」テストバージョンを実行していますが、配列変数を操作して有用なものに変換するのに問題があります。

問題の簡単な説明は、読み取り可能な文字列を出力するためのアラートステートメントを取得できないことです。[object、Object]タイプの出力を取得します。他のコード(図には示されていません)が機能するには、配列に読み取り可能な文字列が必要です。ただし、他の問題は次のとおりです。FirebugConsoleの出力が発生せず、コンソールから次のエラーステートメントが表示されます-エラー:jQueryの2行目からプロパティ'item.autocomplete'にアクセスするためのアクセスが拒否されました。これは、ハードコードされたテストでは発生しません。.grepを使用するのはこれが初めてで、.mapに慣れていないため、問題はこれら3つのセクションでの配列操作にあると確信しています。

ここにいくつかの関連するコードがあります。すべての変数が宣言されていますが、以下にすべての宣言を示しているわけではありません。

citiesWithBoroughs = //a global array variable, populated in a prior select: option

source: function (request, response){
    $.ajax({
        success: function ( data ){
            var geonamesResponse=$.map(data.geonames, function (item){
                return {
                    label: item.name //and others
                }
            }
            alert(citiesWithBoroughs + "," + citiesWithBoroughs.length + "|cWB2" ); //displays correct info
            var noBoroughs=$.grep( geonamesResponse, function ( item, i ) {
                for (var i=0; i < citiesWithBoroughs.length; i++ )
                if( item.label === citiesWithBoroughs[i] ){ //compare  geonamesResponse to each citiesWithBoroughs
                    console.log(citiesWithBoroughs[i]); //nothing in Console
                    return false; //drop any matches out of the geonamesResponse array
                }
                noBoroughs = $.map( data.geonames, function (item){ return item.label; });
                    console.log(noBoroughs); //nothing appears in Console
                return true;        
            });
        alert(noBoroughs.length + "," + citiesWithBoroughs.length + "," + geonamesResponse.length + "|3lengths" ); //correct info
        alert(noBoroughs + "|nB" ); //in test, shows correct number of [object,Object]  but no data
        if( noBoroughs.length != geonamesResponse.length ){
            var dropdownsCityWithBoroughs = $.grep( geonamesResponse, function ( item, i ) {
                for (var i=0; i<citiesWithBoroughs.length; i++ )
                if(item.label === citiesWithBoroughs[i]){return false;}
                return true;
            }, true )//true inverts grep to return the has-Boroughs city in the dropdown
            alert(dropdownsCityWithBoroughs + "|dCWB"); //contains object, Object, but shows no data 
            }                
        }            
    }
}

私は初心者なので、具体的なコメントとコードを教えてください。私は一般的な指示にうまく従いません。

4

1 に答える 1

0

私の問題の簡単な説明は、配列を含むアラートの出力を読み取ることができなかったということでした。JSON.stringify()がそれを修正していることがわかりました。Console.logステートメントが表示されないなどの他の問題は継続しますが、アラートが機能するときにその不可思議な問題に対処できます。

于 2012-12-04T20:37:54.483 に答える