如果存在任何匹配,则两个数组(一个嵌套和一个平面)上的JavaScript部分匹配应返回true

时间:2015-09-22 14:52:53

标签: javascript jquery arrays regex

我需要通过给定数组的每个节点进行部分匹配"结果"。匹配将需要解析原始输入字段中由空格分隔的每个字符串。以下是我基本需要的样本。 请不要插件

我有两组数组,一个结果列表数组和一个输入字段数组:

如果输入搜索术语:" ABC pa 2014" - 现在我存储在一个数组中,所以我可以使用indexOf。但是,它匹配每个空格分隔项的完整字符串。

结果列表数组:

[
  {
    "sIndex": 0,
    "sItem": {
      "cName": "ABC Partners",
      "cId": 0
    }
  },
  {
    "sIndex": 1,
    "sItem": {
      "cName": "ABC Partners",
      "cId": 0,
      "pName": "[ABC] ABC Description",
      "pTick": "ABC",
      "pId": 0,
      "dName": "[2014] ABC Db",
      "dYear": [
        "2014"
      ],
      "dId": 0
    }
  },
  {
    "sIndex": 2,
    "sItem": {
      "cName": "ABC Partners",
      "cId": 0,
      "pName": "[ABC] ABC Description Two",
      "pTick": "ABC",
      "pId": 0,
      "dName": "[2014] ABC Db",
      "dYear": [
        "2014"
      ],
      "dId": 0
    }
  },

用于测试目的的完整数组:

[{"sIndex":0,"sItem":{"cName":"ABC Partners","cId":0}},{"sIndex":1,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0}},{"sIndex":2,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] ABC Partners DB","dYear":"2009","dId":0}},{"sIndex":3,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 1 DB","dYear":"2009","dId":1}},{"sIndex":4,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 1 ExpResults DB","dYear":"2009","dId":2}},{"sIndex":5,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 2 DB","dYear":"2009","dId":3}},{"sIndex":6,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 2 ExpResults DB","dYear":"2009","dId":4}},{"sIndex":7,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 3 BaseDB","dYear":"2009","dId":5}},{"sIndex":8,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 3 DB","dYear":"2009","dId":6}},{"sIndex":9,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 3 ExpResults DB","dYear":"2009","dId":7}},{"sIndex":10,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 4 DB","dYear":"2009","dId":8}},{"sIndex":11,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 4 ExpResults DB","dYear":"2009","dId":9}},{"sIndex":12,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 5 DB","dYear":"2009","dId":10}},{"sIndex":13,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 5 ExpResults DB","dYear":"2009","dId":11}},{"sIndex":14,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2010] Scenario 6 DB","dYear":"2010","dId":12}},{"sIndex":15,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2010] Scenario 6 ExpResults DB","dYear":"2010","dId":13}},{"sIndex":16,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 7 DB","dYear":"2009","dId":14}},{"sIndex":17,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 7 ExpResults DB","dYear":"2009","dId":15}},{"sIndex":18,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 8 BaseDB","dYear":"2009","dId":16}},{"sIndex":19,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 8 DB","dYear":"2009","dId":17}},{"sIndex":20,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 8 ExpResults DB","dYear":"2009","dId":18}}]

该函数应该返回第二个和第三个节点。

如果输入搜索术语:" ABC pa"该函数应该返回所有三个节点。

我为这个问题的简洁而道歉,我们将非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

步骤

  • 按空格将搜索输入拆分为一组单词。
  • 使用Array.prototype.filter方法过滤源数组。
  • 遍历源数组中每个项目的sItem属性的属性,并且:
    • 查看该属性是否为String,如果为true
      • 遍历搜索到的字词以查看是否在该字段中找到它们
  • 如果在属性中找到所有单词,则返回true filter方法

代码

function search(val) {
  val = val.split(' '); // Step 1
  var filtered = arr.filter(function(item) { // Step 2
    var found = []; // Array to save how many words were found
    for (var prop in item.sItem) { // Step 3
      if (typeof item.sItem[prop] === 'string') { // Step 3.1
        for (var i = 0; i < val.length; i++) { // Step 3.1.1
          // Look if the current property has any of the words of the search input
          if (item.sItem[prop].toLowerCase().indexOf(val[i].toLowerCase()) >= 0) {
            found[i] = true; // if found, save to the found array
          }
        }
      }
    }

    // if the true values of the found array has the same length of the input search array
    return found.filter(function(expr) { return expr; }).length == val.length; // Step 4
  });

  // sort the filtered array by the dName property and return it
  return filtered.sort(function(a, b) {
    return b.sItem.dName > a.sItem.dName;
  });
}

见下面的工作

&#13;
&#13;
(function(d) {
  d.getElementById('search').addEventListener('input', function(e) {
    var result = search(this.value);
    d.querySelector('result').innerHTML = JSON.stringify(result, null, 2);
  });

  var arr = [{"sIndex":0,"sItem":{"cName":"ABC Partners","cId":0}},{"sIndex":1,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0}},{"sIndex":2,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] ABC Partners DB","dYear":"2009","dId":0}},{"sIndex":3,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 1 DB","dYear":"2009","dId":1}},{"sIndex":4,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 1 ExpResults DB","dYear":"2009","dId":2}},{"sIndex":5,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 2 DB","dYear":"2009","dId":3}},{"sIndex":6,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 2 ExpResults DB","dYear":"2009","dId":4}},{"sIndex":7,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 3 BaseDB","dYear":"2009","dId":5}},{"sIndex":8,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 3 DB","dYear":"2009","dId":6}},{"sIndex":9,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 3 ExpResults DB","dYear":"2009","dId":7}},{"sIndex":10,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 4 DB","dYear":"2009","dId":8}},{"sIndex":11,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 4 ExpResults DB","dYear":"2009","dId":9}},{"sIndex":12,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 5 DB","dYear":"2009","dId":10}},{"sIndex":13,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 5 ExpResults DB","dYear":"2009","dId":11}},{"sIndex":14,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2010] Scenario 6 DB","dYear":"2010","dId":12}},{"sIndex":15,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2010] Scenario 6 ExpResults DB","dYear":"2010","dId":13}},{"sIndex":16,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 7 DB","dYear":"2009","dId":14}},{"sIndex":17,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 7 ExpResults DB","dYear":"2009","dId":15}},{"sIndex":18,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 8 BaseDB","dYear":"2009","dId":16}},{"sIndex":19,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 8 DB","dYear":"2009","dId":17}},{"sIndex":20,"sItem":{"cName":"ABC Partners","cId":0,"pName":"[ABC] ABC Partners","pTick":"ABC","pId":0,"dName":"[2009] Scenario 8 ExpResults DB","dYear":"2009","dId":18}}]

  function search(val) {
    val = val.split(' ');
    var filtered = arr.filter(function(item) {
      var found = [];
      for (var prop in item.sItem) {
        if (typeof item.sItem[prop] === 'string') {
          for (var i = 0; i < val.length; i++) {
            if (item.sItem[prop].toLowerCase().indexOf(val[i].toLowerCase()) >= 0) {
              found[i] = true;
            }
          }
        }
      }

      return found.filter(function(expr) { return expr; }).length == val.length;
    });
    
    return filtered.sort(function(a, b) {
      return b.sItem.dName > a.sItem.dName;
    });
  }
})(document);
&#13;
result {
  display: block;
  white-space: pre;
  font-family: Courier New;
  font-size: 12px;
}
&#13;
<input type="text" id="search" />
<result></result>
&#13;
&#13;
&#13;