如何使用 Lodash java-script 库进行模糊搜索?
How to make fuzzy search using Lodash java-script library?
我想使用查询字符串对对象数组进行模糊搜索。
搜索可以嵌套在搜索对象中,如下例所示;
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]
我做了一个简单的解决方案,它不是最佳的,但它有效。
这在没有嵌套搜索的模糊搜索分数的情况下完成。
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]
我想使用查询字符串对对象数组进行模糊搜索。 搜索可以嵌套在搜索对象中,如下例所示;
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]
我做了一个简单的解决方案,它不是最佳的,但它有效。 这在没有嵌套搜索的模糊搜索分数的情况下完成。
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]