0
 var options = {
        url: function (phrase) {
            return "location/autoComplete?key=" + phrase;
        },

        getValue: "cityName",

        template: {
            type: "custom",
            method: function (value, item) {
                return value + ",  " + item.stateName;
            }
        },

        list: {

            onClickEvent: function () {
                var selectedLongitude = $("#autoCompleteSearch").getSelectedItemData().longitude;
                var selectedLatitude = $("#autoCompleteSearch").getSelectedItemData().latitude;


                userPos = {
                    lat: Number(selectedLatitude),
                    lng: Number(selectedLongitude)
                };
                map.setCenter(userPos);
                map.setZoom(17)
            },

            showAnimation: {
                type: "fade", //normal|slide|fade
                time: 400,
                callback: function () {
                }
            },

            hideAnimation: {
                type: "slide", //normal|slide|fade
                time: 400,
                callback: function () {
                }
            }
        }
    };

这是我现在用来获取远程 Web 服务数据的代码。现在我需要获取数据并删除重复的“城市名称”。当网络服务没有数据时,我还需要显示“数据不可用”或一些自定义方法。我有点卡住了。如果有人能指出我如何实现这一点,那就太好了。

4

1 回答 1

0

您可以使用助手删除重复项function

function removeDuplicates(input) {
    if (!input) { //Falsy input
        return input;
    }
    var isArray = Array.isArray(input) ? [] : {};
    var items = [];
    var output = {};
    for (var key in input) {
        if (items.indexOf(input[key]) < 0) {
            items.push(input[key]);
            if (!isArray) {
                output[key] = input[key];
            }
        } 
    }
    return isArray ? items : output;
}

如果Array.isArray(input) ? (input.length === 0) : (Object.keys(input).length === 0 && input.constructor === Object).

于 2017-05-12T10:31:54.837 回答