2017-04-24 51 views
0

当前,函数仅显示基于2个变量的数组的1个正确结果。向此JS搜索添加部分和多个结果功能

阵列样本数据

[{"username": "lromero2l", "first_name": "Lawrence", "last_name": "Romero", "gender": "Male", "sexuality": "Public-key value-added array", "bestFriend": "loperamide HCl"}, {"username": "glopez2o", "first_name": "Gloria", "last_name": "Lopez", "gender": "Female", "sexuality": "Balanced multi-tasking time-frame", "bestFriend": "Aconite, Arnica, Calendula, Hypericum, Ledum, Ruta Grav."}, {"username": "ereyes2p", "first_name": "Elizabeth", "last_name": "Reyes", "gender": "Female", "sexuality": "Future-proofed systemic infrastructure", "bestFriend": "Tretinoin"}, {"username": "ppalmer2q", "gender": "Male", "sexuality": "Optimized multi-tasking circuit", "bestFriend": "Pantoprazole Sodium"}, {"username": "harmstrong2r", "first_name": "Helen", "last_name": "Armstrong", "gender": "Female", "sexuality": "Seamless zero tolerance interface", "bestFriend": "ERYTHROMYCIN"}] 

搜索功能

function myFunction(e) { 
    if((e.target.id === 'mySearch' && e.keyCode === 13) || e.target.id === 'searchButton'){ 
     e.preventDefault(); 
     var searchValue = document.getElementById("mySearch").value; 
     for(var i = 0; i < users.length; i++){ 
     if(users[i]['bestFriend'] === searchValue || (users[i]['username'] === searchValue)){ 
     document.getElementById("usernameOut").innerHTML = ("USERNAME" + '<br/>' + users[i].username); 
     document.getElementById("firstNameOut").innerHTML = ("FIRST NAME" + '<br/>' + users[i].first_name); 
     document.getElementById("lastNameOut").innerHTML = ("LAST NAME" + '<br/>' + users[i].last_name); 
     document.getElementById("genderOut").innerHTML = ("GENDER" + '<br/>' + users[i].gender); 
     document.getElementById("sexualityOut").innerHTML = ("SEXUALITY" + '<br/>' + users[i].sexuality); 
     document.getElementById("friendOut").innerHTML = ("BEST FRIEND" + '<br/>' + users[i].bestFriend); 
     displayImage(); 
     document.getElementById("main_text").style.display = "none"; 
     document.getElementById("return").style.display = "block"; 
     return; 
     } 
    } 
    } 
} 

我想在功能显示部分结果。即如果4个以上的字符正确,则显示整个结果。

我还需要显示多个结果的功能。 (也就是说,如果该术语与“bestFriend”或“username”值相似,则显示)。这是否意味着将“ID”重组为“Class”?我试图用if(users[i]['bestFriend'] === searchValue || (users[i]['username'] === searchValue))摆弄,以便让搜索词更具回旋余地,但一切似乎都打破了这个功能。该功能的哪些部分需要更改/我需要添加哪些内容才能实现此目的?

请不要犹豫,要求这个被编辑。我很尴尬,对于SO还是有点新鲜的。谢谢!

+0

我在这里看到的唯一问题是*“这是否意味着重构将'ID'改为'Class'?“ - 当然,尝试维护一堆唯一的ID肯定是一个更好的主意,但是你没有*。 – Santi

+0

@Santi谢谢。我稍微改变了请求。 – gezer4000

回答

0

下面是如何利用Levenshtein距离来实现这一点的简短示例。

Levenshtein距离(LD)是两个字符串之间的相似性度量,我们将其称为源字符串和目标字符串(t)。距离是将s转换为t所需的删除,插入或替换的数量。从这里取

距离函数:https://gist.github.com/andrei-m/982927

编辑:重新阅读你的问题让我意识到这不是正是你问什么,和一个简单的SUBSTR比赛将在这里足以

/* 
 
Copyright (c) 2011 Andrei Mackenzie 
 
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
 
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 
*/ 
 

 
// Compute the edit distance between the two given strings 
 
function distance(a, b) { 
 
    if (a.length == 0) return b.length; 
 
    if (b.length == 0) return a.length; 
 

 
    var matrix = []; 
 

 
    // increment along the first column of each row 
 
    var i; 
 
    for (i = 0; i <= b.length; i++) { 
 
    matrix[i] = [i]; 
 
    } 
 

 
    // increment each column in the first row 
 
    var j; 
 
    for (j = 0; j <= a.length; j++) { 
 
    matrix[0][j] = j; 
 
    } 
 

 
    // Fill in the rest of the matrix 
 
    for (i = 1; i <= b.length; i++) { 
 
    for (j = 1; j <= a.length; j++) { 
 
     if (b.charAt(i - 1) == a.charAt(j - 1)) { 
 
     matrix[i][j] = matrix[i - 1][j - 1]; 
 
     } else { 
 
     matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution 
 
      Math.min(matrix[i][j - 1] + 1, // insertion 
 
      matrix[i - 1][j] + 1)); // deletion 
 
     } 
 
    } 
 
    } 
 

 
    return matrix[b.length][a.length]; 
 
}; 
 

 

 

 
const users = [{ 
 
    "username": "lromero2l", 
 
    "first_name": "Lawrence", 
 
    "last_name": "Romero", 
 
    "gender": "Male", 
 
    "sexuality": "Public-key value-added array", 
 
    "bestFriend": "loperamide HCl" 
 
}, { 
 
    "username": "glopez2o", 
 
    "first_name": "Gloria", 
 
    "last_name": "Lopez", 
 
    "gender": "Female", 
 
    "sexuality": "Balanced multi-tasking time-frame", 
 
    "bestFriend": "Aconite, Arnica, Calendula, Hypericum, Ledum, Ruta Grav." 
 
}, { 
 
    "username": "ereyes2p", 
 
    "first_name": "Elizabeth", 
 
    "last_name": "Reyes", 
 
    "gender": "Female", 
 
    "sexuality": "Future-proofed systemic infrastructure", 
 
    "bestFriend": "Tretinoin" 
 
}, { 
 
    "username": "ppalmer2q", 
 
    "gender": "Male", 
 
    "sexuality": "Optimized multi-tasking circuit", 
 
    "bestFriend": "Pantoprazole Sodium" 
 
}, { 
 
    "username": "harmstrong2r", 
 
    "first_name": "Helen", 
 
    "last_name": "Armstrong", 
 
    "gender": "Female", 
 
    "sexuality": "Seamless zero tolerance interface", 
 
    "bestFriend": "ERYTHROMYCIN" 
 
}]; 
 

 
const max_distance = 3 
 

 
const renderValue = ([key, val]) => `<div><span>${key}:</span><span> ${val}</span></div>` 
 

 
const renderUser = (user) => `<div class="user">${Object.entries(user).map(renderValue).join('')}</div>` 
 

 
const checkValue = (term, value) => distance(term, value) <= max_distance 
 

 
const search = (term) => users.filter(item => 
 
    Object.values(item) 
 
    .filter(value => checkValue(term, value)).length > 0) // searches all object values for a match 
 

 
const render = (term) => { 
 
    const html = search(term) 
 
    .map(renderUser) 
 
    .join('') 
 

 
    document.querySelector('.users').innerHTML = html; 
 
} 
 

 
document.querySelector('.query') 
 
    .addEventListener('input', (e) => render(e.target.value))
div { 
 
    display: block 
 
} 
 

 
.user { 
 
    padding: 5px 
 
}
<input type="text" class="query" placeholder="search me"/> 
 
<div class='users'></div>