我在使用 jQuery 和排序 JSON 数组时遇到了一点问题。
我要做的是根据用户选择的时间过滤 geoJSON 数组。 用户可以使用 jQuery slider 选择小时范围。
$("#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"
}
}
]}
最佳答案
使用jQuery.grep(array, function(elementOfArray, indexInArray) ) 函数过滤数组。
然后使用带有自定义比较函数的 javascript 排序函数按 visitedTimes 排序。
关于javascript - 根据用户选择过滤 JSON 嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7809707/