如何使用Lodash java-script库进行模糊搜索?

时间:2016-09-10 18:41:28

标签: javascript lodash fuzzy-search

我想使用查询字符串从对象数组进行模糊搜索。 搜索可以嵌套在搜索对象中,如下例所示;



var data = [ 
              {
                "id":"1",
                "name":"Ali",
                "BOD":"29/10/2055",
                "type":"primary",
                "email":"b@v.com",
                "mobile":"0100000000",
                "notes":["note1" ,"note2" ,"note3"]
              },
               {
                "id":"2",
                "name":"Tie",
                "BOD":"29/10/2055",
                "type":"primary",
                "email":"b@v.net",
                "mobile":"0100000000",
                "notes":["note4" ,"note5" ,"note6"]
              }
  ];


   function search(query){
     // search code go here
     
     }

   // search examples
   search('.net'); //expected data[1]
   search('ali');  //expected data[0]
   search('0110'); //expected data[0],data[1]




1 个答案:

答案 0 :(得分:2)

我做了一个简单的解决方案,它不是最优的,但它的工作原理。 这样做没有嵌套搜索的模糊搜索得分。



int main(int argc, const char * argv[]) {
  @autoreleasepool {

    NSUInteger t;
    scanf("%ld",&t);

    int n,m,s;
    int i = 0;
    while (i < t) {
      scanf("%d %d %d",&n,&m,&s);
      printf("%d  n=%d m=%d s=%d \n",i,n,m,s);
      i++;
    }
  }
  return 0;
}
&#13;
&#13;
&#13;