我需要通过给定数组的每个节点进行部分匹配"结果"。匹配将需要解析原始输入字段中由空格分隔的每个字符串。以下是我基本需要的样本。 请不要插件
我有两组数组,一个结果列表数组和一个输入字段数组:
如果输入搜索术语:" 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"该函数应该返回所有三个节点。
我为这个问题的简洁而道歉,我们将非常感谢任何帮助。
答案 0 :(得分:0)
Array.prototype.filter
方法过滤源数组。sItem
属性的属性,并且:
String
,如果为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;
});
}
(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;