如何从对象搜索中检索键和值?
how to retrieve key and value from object search?
function pageKeywords(searchValue){
const pageKeywords = {
"home": "/homepage",
"about": "/about-us",
"about us": "/about-us"
}
const getInputVal = searchValue;
if (pageKeywords[getInputVal]) {
console.log(pageKeywords[getInputVal])
window.location.href = pageKeywords[getInputVal];
} else {
console.log('not found')
}
}
$(document).ready(function(){
$("#searchKeywords").on("keypress", function(e) {
if (e.which === 13) {
pageKeywords($(this).val())
}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="searchKeywords">
我想找到一个页面 url 通过输入搜索。我有一个包含关键字和 url 的对象。然后当回车时会检查,如果关键字相同或存在,则显示url,否则显示未找到。
因此,例如在输入中我输入“home”,当我输入时它将显示 url“/homepage”,因为关键字存在,如果我输入“contact”,它将显示未找到因为关键字不存在。
我已经制作了如下代码,但为什么找不到也出现了?
$(document).ready(function(){
$("#searchKeywords").on("keypress", function(e){
if(e.which == 13){
const map = {
"home" : "/homepage",
"about" : "/about-us"
}
const searchVal = $("#searchKeywords").val()
Object.entries(map).map((k,v) => {
if(searchVal == k[0]){
console.log(k[1])
}else{
console.log("not found")
}
});
}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="searchKeywords">
这里pageKeywords
是一个对象。所以你可以直接取回key,而不需要使用map和Object.entries。另请注意,您可以在按键事件侦听器之外初始化对象,并且更喜欢使用 ===
而不是 ==
$(document).ready(function() {
const pageKeywords = {
"home": "/homepage",
"about": "/about-us"
}
$("#searchKeywords").on("keypress", function(e) {
if (e.which === 13) {
const getInputVal = $(this).val().toLowerCase();
if (pageKeywords[getInputVal]) {
console.log(pageKeywords[getInputVal])
} else {
console.log('not found')
}
}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="searchKeywords">
function pageKeywords(searchValue){
const pageKeywords = {
"home": "/homepage",
"about": "/about-us",
"about us": "/about-us"
}
const getInputVal = searchValue;
if (pageKeywords[getInputVal]) {
console.log(pageKeywords[getInputVal])
window.location.href = pageKeywords[getInputVal];
} else {
console.log('not found')
}
}
$(document).ready(function(){
$("#searchKeywords").on("keypress", function(e) {
if (e.which === 13) {
pageKeywords($(this).val())
}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="searchKeywords">
我想找到一个页面 url 通过输入搜索。我有一个包含关键字和 url 的对象。然后当回车时会检查,如果关键字相同或存在,则显示url,否则显示未找到。
因此,例如在输入中我输入“home”,当我输入时它将显示 url“/homepage”,因为关键字存在,如果我输入“contact”,它将显示未找到因为关键字不存在。
我已经制作了如下代码,但为什么找不到也出现了?
$(document).ready(function(){
$("#searchKeywords").on("keypress", function(e){
if(e.which == 13){
const map = {
"home" : "/homepage",
"about" : "/about-us"
}
const searchVal = $("#searchKeywords").val()
Object.entries(map).map((k,v) => {
if(searchVal == k[0]){
console.log(k[1])
}else{
console.log("not found")
}
});
}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="searchKeywords">
这里pageKeywords
是一个对象。所以你可以直接取回key,而不需要使用map和Object.entries。另请注意,您可以在按键事件侦听器之外初始化对象,并且更喜欢使用 ===
而不是 ==
$(document).ready(function() {
const pageKeywords = {
"home": "/homepage",
"about": "/about-us"
}
$("#searchKeywords").on("keypress", function(e) {
if (e.which === 13) {
const getInputVal = $(this).val().toLowerCase();
if (pageKeywords[getInputVal]) {
console.log(pageKeywords[getInputVal])
} else {
console.log('not found')
}
}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="searchKeywords">