1

有一个标记数组,每个标记属于一个或多个类别。

您将如何实现搜索/过滤器来检索所有具有 cat = X 的标记?

例如:

  • 所有带有 cat = 9 的标记?
  • 所有带有 cat = 5 和 9 的标记?
Gmaps.map.markers = [
    {"id":9,"cat":"3, 5, 6","lat":"52.50","lng":"13.32"},
    {"id":11,"cat":"3","lat":"52.51","lng":"13.44"},
    {"id":12,"cat":"1, 2, 5","lat":"47.89","lng":"10.62"},
    {"id":13,"cat":"1, 2, 3, 4, 5","lat":"47.77","lng":"10.61"},
    {"id":15,"cat":"1, 6","lat":"40.60","lng":"-73.44"},
    {"id":16,"cat":"1, 3, 5, 9","lat":"51.89","lng":"17.16"},
    {"id":17,"cat":"9","lat":"52.37","lng":"9.73"}
];
4

2 回答 2

0

修改它以找到猫 4 以及...

function findMarkersContaining ( markers, needleArray ) {
  var matches = [];
  var markerLength = markers.length;
  var needleArrayLength = needleArray.length;
    for ( var i = 0; i < markerLength; i++ ) {
      for ( var e = 0; e < needleArrayLength; e++ ) {
        var singleNeedle = needleArray[e];
        if ( $.inArray( singleNeedle, markers[i].cat.split(",")) !== -1 ) {
           matches.push( markers[i] );
        }
      }
    }
  return matches;
 };
于 2012-08-09T23:57:00.243 回答
0

像这样的东西使用正则表达式来查找是否在 cat 逗号分隔列表中找到了 needleArray 中的某些内容,如果是,则将该标记添加到返回的数组中。

function findMarkersContaining ( markers, needleArray ) {
   var matches = [];
   var markerLength = markers.length;
   var needleArrayLength = needleArray.length;
   for ( var i = 0; i < markerLength; i++ ) {
      for ( var e = 0; e < needleArrayLength; e++ ) {
         var regex = new RegExp( '(^| )' + needleArray[e] + '(,|$)' );
         if ( markers[i].cat.search( regex ) !== -1 ) {
            matches.push( markers[i] );
         }
      }
   }
   return matches;
}
于 2012-08-09T15:37:06.103 回答