【问题标题】:Filtering a JSON nested array based on user selection根据用户选择过滤 JSON 嵌套数组
【发布时间】:2011-12-10 05:11:54
【问题描述】:

我在使用 jQuery 和对 JSON 数组进行排序时遇到了一点问题。

我想做的是根据用户选择的小时过滤geoJSON数组。 用户可以使用 jQuery 滑块选择小时范围。

    $("#slider-range").slider({
        stop: function(event, ui) {
            $("#amount").val("h" + ui.values[0]+":00" + 
                             "h" + ui.values[1]+":00");
            minSel =ui.values[0];
            maxSel =ui.values[1];

            //HERE SHOULD BE PLACED THE FILTER FUNCTION
            filterArray(minSel,maxSel);
        }
    });

如何仅返回按其visitedTimes 排序的小时选择之间的数组值?

下面是数组的一个示例。

谁能帮帮我?

{ "type": "FeatureCollection", "features": [   
 {   "type": "Feature",
    "id": "...",
    "geometry": { "type": "Point", "coordinates": [...]},
    "properties": {    "venueName": "Its name",

                       "visitedTimes": "1", 
                       "day": "07",
                       "month": "09",
                       "hour": "00",
                       "min": "50",
                       "sec": "58"
    }
},
{   "type": "Feature",
    "id": "4bd34eca462cb7133d1dde07",
    "geometry": { "type": "Point", "coordinates": [...]},
    "properties": { "venueName": "Old Wild West",
                       "visitedTimes": "4", 
                       "day": "07",
                       "month": "09",
                       "hour": "00",
                       "min": "51",
                       "sec": "21"
    }
},
{   "type": "Feature",
    "id": "...",
    "geometry": { "type": "Point", "coordinates": [...]},
    "properties": { "venueName": "Past & future",
                       "day": "07",
                       "month": "09",
                       "hour": "00",
                       "min": "51",
                       "sec": "23"
    }
}
]}

【问题讨论】:

  • 我假设您要将 JSON 解析为 JavaScript 对象,因此您的问题实际上与 JSON 无关。

标签: javascript jquery arrays json filtering


【解决方案1】:

使用 jQuery.grep(array, function(elementOfArray, indexInArray) ) 函数过滤数组。

然后使用带有自定义比较器函数的javascript排序函数按visitedTimes排序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-06
    • 2018-05-25
    • 1970-01-01
    • 2020-03-11
    • 2022-12-07
    • 1970-01-01
    • 2014-08-19
    • 2017-08-24
    相关资源
    最近更新 更多