2

I want to make fuzzy search from array of objects using query string . Search may be nested in search object like the following example ;

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]
Amr Ibrahim
  • 2,066
  • 2
  • 23
  • 47

1 Answers1

2

I made simple solution it is not that optimal but it works . this done without score of fuzzy search for nested searching .

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

    /**
     * query: query string to match with
     * dataArray: data array variable, array or opject to search it
     **/
   function search(query,dataArray){
     // search code go here
     //console.log(query);
     var matched = [];
     //init null values
     if(!dataArray)dataArray={};
     if(!query)query='';
     dataArray.forEach(function(obj,index){
        for(var key in obj){
          if(!obj.hasOwnProperty(key) || !obj[key]) continue;
          if(obj[key].toString().indexOf(query) !== -1)
            {
              matched.push(obj );
            } 
        }
     });
     return matched ;      
     }

   // search examples .
  console.log("found",search('.net',data).length);//expected data[0] data[1]
  console.log("found",search('Ali',data).length);//expected data[0]
  console.log("found",search('0116',data).length);//expected data[0],data[1] 
Amr Ibrahim
  • 2,066
  • 2
  • 23
  • 47