简体   繁体   English

如何创建查找数组中匹配数据的搜索?

[英]How do I create a search which looks for matching data in an array?

I want to create a search box which checks the input and sees if it matches a name of a town. 我想创建一个搜索框,检查输入内容并查看是否与城镇名称匹配。

My data is organised like so (fictitious data btw): 我的数据是这样组织的(虚拟数据顺便说一下):

var markers = {
    towns: {
        "London": {lat: -83.68088192646843, lng: -125.270751953125},
        "Paris": {lat: -58.1548020417031, lng: -21.318115234375},
    }
}

So to clarify: 所以要澄清一下:

  • Take the textbox input, var query 接受文本框输入, var query
  • Search through the towns, var results 搜索城镇, var results
  • if (query === results) do something eg make an alert if (query === results)做某事,例如发出警报

Thanks. 谢谢。

Edit: 编辑:

I have been able to make a test as follows: 我已经能够进行如下测试:

$("#search").blur(function() {
    var query = $("#search").val();
    var results = "hi";
    if (query === results) {
        alert("booyah");
    }   
})

This works, however as you can see I don't know how to store the towns in var results . 这可行,但是如您所见,我不知道如何在var results存储城镇。

Edit #2: Would this work? 编辑2:这行得通吗?

$("#search").blur(function() {
    var query = $("#search").val();
    for (var town in markers.towns) {
        if (query !== markers.towns[town]) {
            alert("booyah");
        }           
    }
})

first of all, your object is not an array. 首先,您的对象不是数组。 this case could be quite simple: 这种情况可能非常简单:

var coordinates = (function () {
    var markers = {
        towns: {
            "London": {lat: -83.68088192646843, lng: -125.270751953125},
            "Paris": {lat: -58.1548020417031, lng: -21.318115234375},
        }
    };
    return {
        find = function (city) {
             markers.towns[city] || null;
        }
    };
})();

the find method will return your value object or null. find方法将返回您的value对象或null。

When I run the following, using a hard-coded query variable, it works fine: 当我使用硬编码的查询变量运行以下命令时,它工作正常:

var markers = {
    towns: {
        "London": {lat: -83.68088192646843, lng: -125.270751953125},
        "Paris": {lat: -58.1548020417031, lng: -21.318115234375}
    }
}

var query = "london";   // Test with different capitalization.
for (var town in markers.towns) {
    // If the current town name matches the input field (case insensitive)
    if (town.toLowerCase() === query.toLowerCase()) {
        alert("booyah");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在原始数据中搜索匹配数据? - How do I search through raw data for matching data? 如何在xml数据的数组索引中搜索匹配的字符串? - How can I search array index of xml data for a matching string? 如何创建一个下拉菜单,将菜单中的数据放入文本字​​段中 - How do i create a dropdown menu which puts data from an array in a textfield 如何创建一个看起来像漂浮在 div 上的表单? - How do I create a form that looks like it's floating on a div? 如何为二维数组搜索创建 function? - How do I create a function for two-dimensional array search? 如何使用拆分为数组的字符串搜索数组? - How do I search through an array using a string, which is split into an array? 如何创建多维数组,该数组的维数由字符串的长度确定,以创建数组 - How do I create a Multidimensional Array which the dimension is determined by the length of the strings to create the array 如何在一组数据项中搜索特定匹配项? - How to search an array of data items for a specifically matching item? 如何创建 div 的数组结构我匹配的 div id 与 data-id 这是其他子 div 的 id? - How to create array structure of div my matching div id with data-id which is id of other child div? 如何创建对象数组以将虚拟数据加载到dynatree中? - How do I create an object array to load dummy data into dynatree?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM