目前,该函数仅显示基于2个变量的数组的1个正确结果。
数组样本数据
[{"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个字符都正确,则显示整个结果。
我还需要显示多个结果的功能。 (即,如果该术语与“最佳朋友”或“用户名”值相似,则显示。)这是否意味着重组&#39; ID&#39;到&#39;班级&#39;?我试图摆弄if(users[i]['bestFriend'] === searchValue || (users[i]['username'] === searchValue))
以便在搜索条件上留有更多余地,但一切似乎都破坏了这个功能。该功能的哪个部分需要更改/我需要添加什么来实现这一目标?
请不要犹豫,要求对其进行编辑。我非常狡猾,对SO来说仍然有点新鲜。谢谢!
答案 0 :(得分:0)
以下是关于如何利用Levenshtein距离来实现这一目标的简短示例。
Levenshtein距离(LD)是两个字符串之间相似性的度量,我们将其称为源字符串和目标字符串(t)。距离是将s转换为t所需的删除,插入或替换的数量。
从这里取得的距离函数:https://gist.github.com/andrei-m/982927
编辑:再次阅读你的问题让我意识到这不是你要求的,一个简单的子匹配就足够了
/*
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))
&#13;
div {
display: block
}
.user {
padding: 5px
}
&#13;
<input type="text" class="query" placeholder="search me"/>
<div class='users'></div>
&#13;