2016-10-26 58 views
1

我想根据我在输入字段中输入的内容获得结果。此搜索可以通过多个键过滤,如firstName,lastName,emailId。如何通过在javascript中搜索多个属性值来实现过滤器过滤器?

这是我对象的数组,

var resData = [ 
      { 
       firstName:"Jhon", 
       lastName:"adam", 
       emailId:"[email protected]" 
      }, 
      { 
       firstName:"Kyle", 
       lastName:"Miller", 
       emailId:"[email protected]" 
      }, 
      { 
       firstName:"Jhonathan", 
       lastName:"adam", 
       emailId:"[email protected]" 
      }, 
      { 
       firstName:"Lewis", 
       lastName:"harber", 
       emailId:"[email protected]" 
      } 
     ]; 

Javascript代码,

resData.filter(data, function(item){ 
         item.map(function(list, i) { 
          if (list.firstName.toLowerCase().indexOf(self.state.inputValue.toLowerCase()) === -1 || list.lastName.toLowerCase().indexOf(self.state.inputValue.toLowerCase()) === -1 || list.emailId.toLowerCase().indexOf(self.state.inputValue.toLowerCase()) === -1) { 
           return; 
          } 
          console.log(list); 
         }); 

        }); 
+0

用户应该如何选择多个值?有没有格式,例如'firstname:Jhon emaiid:jhn12 @ gmail.com'?或者它只是一个以空格分隔的字符串列表:'jhon jhn12 @ gmail.com'? –

+0

听起来就像你想自动完成寻找所有值的匹配。这个'map'在那里做什么? – sabithpocker

回答

3

你可以通过按键和迭代,如果某个值时发现过滤器返回true。

var resData = [{ firstName: "Jhon", lastName: "adam", emailId: "[email protected]" }, { firstName: "Kyle", lastName: "Miller", emailId: "[email protected]" }, { firstName: "Jhonathan", lastName: "adam", emailId: "[email protected]" }, { firstName: "Lewis", lastName: "harber", emailId: "[email protected]" }], 
 
    self = { state: { inputValue: 'adam' } }, 
 
    result = resData.filter(function (item) { 
 
     return Object.keys(item).some(function (k) { 
 
      return item[k].toLowerCase().indexOf(self.state.inputValue.toLowerCase()) !== -1; 
 
     }); 
 
    }); 
 
    
 
console.log(result);

随着预设按键

var resData = [{ firstName: "Jhon", lastName: "adam", emailId: "[email protected]" }, { firstName: "Kyle", lastName: "Miller", emailId: "[email protected]" }, { firstName: "Jhonathan", lastName: "adam", emailId: "[email protected]" }, { firstName: "Lewis", lastName: "harber", emailId: "[email protected]" }], 
 
    self = { state: { inputValue: 'adam' } }, 
 
    result = resData.filter(function (item) { 
 
     return ['firstName', 'lastName'].some(function (k) { 
 
      return item[k].toLowerCase().indexOf(self.state.inputValue.toLowerCase()) !== -1; 
 
     }); 
 
    }); 
 
    
 
console.log(result);

+0

我可以手动定义密钥吗?由于某些关键值为空。所以它返回的错误。 – Sathya

+0

是的,你可以。请参阅编辑。 –

1
var resData = [ 
     { 
      firstName:"Jhon", 
      lastName:"adam", 
      emailId:"[email protected]" 
     }, 
     { 
      firstName:"Kyle", 
      lastName:"Miller", 
      emailId:"[email protected]" 
     }, 
     { 
      firstName:"Jhonathan", 
      lastName:"adam", 
      emailId:"[email protected]" 
     }, 
     { 
      firstName:"Lewis", 
      lastName:"harber", 
      emailId:"[email protected]" 
     } 
    ]; 

var search = function(text) { 
    text = text.toLowerCase(); 
    return resData.filter(x => x.firstName.toLowerCase().indexOf(text) >= 0 
     || x.lastName.toLowerCase().indexOf(text) >= 0 
     || x.emailId.toLowerCase().indexOf(text) >= 0); 
} 

console.log(search("Adam")); 
+0

尽管在性能和可读性方面* waaay *优于OP的版本,但并不确定它实际上是否回答了问题。 –

+0

如何?这将返回与基于不同键值的搜索文本相匹配的元素。这是我对他的问题的理解。 – chanoto89

+0

是的,这就是问题所在,根本不清楚问题*是什么。 –

0

看起来你想通过任意键搜索...

var resData = [{ 
 
    firstName: "Jhon", 
 
    lastName: "adam", 
 
    emailId: "[email protected]" 
 
}, { 
 
    firstName: "Kyle", 
 
    lastName: "Miller", 
 
    emailId: "[email protected]" 
 
}, { 
 
    firstName: "Jhonathan", 
 
    lastName: "adam", 
 
    emailId: "[email protected]" 
 
}, { 
 
    firstName: "Lewis", 
 
    lastName: "harber", 
 
    emailId: "[email protected]" 
 
}]; 
 

 
//var searchString = self.state.inputValue.toLowerCase(); 
 
var searchString = "adam".toLowerCase(); 
 

 
// filter the data 
 
var filteredData = resData.filter(function(item) { 
 
    // for each item int he array, check each key value 
 
    for (key in item) { 
 
    // if the key value matches the search string then return true 
 
    if (item[key].toLowerCase() == searchString) return true; 
 
    } 
 
    // if we get here that means none of the values in the item were a match so return false 
 
    return false; 
 
}); 
 

 
console.log(filteredData);