【问题标题】:Live search localstorage (search in array)实时搜索 localstorage(在数组中搜索)
【发布时间】:2013-04-15 07:49:30
【问题描述】:

这就是我所拥有的:

现在我想进行实时搜索。关键是当您在文本框中键入时,您必须自动在本地存储中搜索与搜索框中的文本相同的内容。所有不匹配的数据都会消失。

$("#searchbox").keyup(function(){

    // Retrieve the input field text
    var searchtext = $(this).val();

    // Loop through the local storage
    var a = {};
    a = JSON.parse(localStorage.getItem('session'));

    alert(a);
});

如您所见,我发出警报,我的输出是这样的:

{"21114":{"id":"21114","external_id":"","sessiongroupid":"1844","eventid":"5588","order":"0","name":"localStorage
 HTML5 Session","description":"localstorage","starttime":"2013-04-23
 12:00:00","endtime":"2013-04-23
 13:30:00","speaker":"","location":"","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":"","venueid":"0"},"21115":{"id":"21115","external_id":"","sessiongroupid":"1845","eventid":"5588","order":"0","name":"tweede","description":"tweede","starttime":"2013-04-03
 00:00:00","endtime":"2013-04-04
 00:00:00","speaker":"","location":"","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":"","venueid":"0"}}

现在我想搜索名称。有人可以帮我怎么做吗?

【问题讨论】:

    标签: javascript jquery arrays search object


    【解决方案1】:

    简单地遍历对象

    var json = JSON.parse(localStorage);
    
    for(obj in json) {
        console.log(json[obj].name); //compare this with your "searchtext"
    }
    

    打印

    localStorage HTML5 Session
    tweede
    

    如果.name 匹配您想要的任何内容,您可以简单地检查循环内部并进行处理。

    【讨论】:

      【解决方案2】:

      试试这个简单的代码

      dataArray.filter(function(object) {
         return object.name == new RegExp('text to search', 'gi')
      })
      

      用你想要搜索的键更改.name

      【讨论】:

        猜你喜欢
        • 2021-05-26
        • 2015-03-20
        • 2020-02-01
        • 2013-07-13
        • 2015-04-11
        • 2012-06-11
        • 1970-01-01
        • 2020-08-17
        • 2014-04-13
        相关资源
        最近更新 更多