0

我在 source: 选项中有一个使用 geonames.org 城市的自动完成功能,并且想要操纵结果中的一些数组。我有一个“硬编码”测试版本正在运行,但是在操作数组变量以将其变成有用的东西时遇到了麻烦。

问题的简短陈述是我无法让警报语句输出可读字符串。我得到 [object, Object] 类型的输出。我需要数组中的可读字符串才能使其他代码(未显示)工作。但其他问题是:Firebug 控制台输出没有发生,并且控制台给了我以下错误语句 - 错误:访问属性 'item.autocomplete' 的权限被拒绝 - 来自 jQuery 的第 2 行。硬编码测试不会发生这种情况。这是我第一次使用 .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 回答