如何在所有数组元素中进行搜索过滤

时间:2019-02-06 18:01:54

标签: javascript

我有这个

rows=[
  {label1 : "abc", label2 : "bcd"}, 
  {label3 : "cde", label4 : "dce"}
 ]

我尝试过

if(this.query.length > 1){  
 rows = rows.filter(row=>row.label1.toLowerCase().includes(this.query))
}  

但是当我开始查询时,我不仅需要搜索所有“ rows []”标签,还需要搜索1个 像

rows = rows.filter(row=>row.label1.toLowerCase().includes(this.query))&& 
   rows.filter(row=>row.label2.toLowerCase().includes(this.query))... etc

当我搜索字母“ b”之间的常见字母时,我希望得到例如label1和label2结果

3 个答案:

答案 0 :(得分:5)

您可以将someObject.values混合以搜索对象中的所有值:

let rows=[
  {label1 : "a", label2 : "b"}, 
  {label3 : "c", label4 : "d"}
 ]

function findInValues(arr, filter){
  return arr.filter(obj => Object.values(obj).some(v => v.includes(filter)))

}

 console.log(findInValues(rows, 'd'))
 console.log(findInValues(rows, 'a'))
 console.log(findInValues(rows, 'notHere'))

答案 1 :(得分:2)

您可以实现二进制搜索。这是基础知识,但您可以修改<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot "C:/wamp64/www/lsapp/public" ServerName lsapp.test ServerAlias *.lsapp.test <Directory "C:/wamp64/www/lsapp/public"> Options All AllowOverride All Require local </Directory> </VirtualHost> 使其递归工作。

更新:我为嵌套树创建了a recursive version

filterRecord
const OPERANDS = [ 'and', 'or' ]; // Not used

// You need to convert the string to a binary tree or use the one below.
var rawQuery = 'label3="c" and label4="d"';
var query = {
  'and' : {
    'label3' : 'c',
    'label4' : 'd'
  }
}

var data = [
  { label1: "a", label2: "b" },
  { label3: "c", label4: "d" }
];

console.log(filter(data, query));

function filter(data, query) {
  return query != null ? data.filter(record => filterRecord(record, query)) : data;
}

function filterRecord(record, query) {
  return Object.keys(query).some((operand) => {
    var args = Object.keys(query[operand]);
    var leftKey = args[0];
    var leftVal = query[operand][args[0]]; // add check if object and recurse
    var rightKey = args[1];
    var rightVal = query[operand][args[1]]; // add check if object and recurse
    var fields = Object.keys(record);
    switch (operand) {
      case 'or':
        if (fields.includes(leftKey) || fields.includes(rightKey)) {
          return record[leftKey] === leftVal || record[rightKey] === rightVal;
        }
      case 'and':
        if (fields.includes(leftKey) && fields.includes(rightKey)) {
          return record[leftKey] === leftVal && record[rightKey] === rightVal;
        }
    }
    return false;
  });
}

答案 2 :(得分:1)

您希望它随后过滤所有键。我假设您事先不知道键,因为那样的话,您只需要在过滤条件下对其进行硬编码即可。即

rows=[
  {label1 : "a", label2 : "b"}, 
  {label3 : "c", label4 : "d"}
 ]

if (this.query) {
  rows = rows.filter(row => row.label1.toLowerCase().includes(this.query) || row.label2.toLowerCase().includes(this.query))
}

如果您需要动态它,则可以在对象的数组上使用some方法(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)。尝试这样的事情:

rows=[
  {label1 : "a", label2 : "b"}, 
  {label3 : "c", label4 : "d"}
 ]

if (this.query) {
  rows = rows.filter(row => Object.keys(row).some(k => row[k].toLowerCase().includes(this.query))
}

注意:我看到有人殴打我。由于他们将逻辑抽象为一个函数,而这个没有,因此我将继续进行。也许会有助于理解逻辑。