js json 对象 全文搜索

let data = [{
    "id": 31,
    "city_id": 13,
    "title": "五村名",
    "description": "",
    "status_id": 4,
    "latitude": "31.238146",
    "longitude": "121.414987",
    "address": "曹杨五村",
    "content": "",
    "image": null,
    "visible": 1,
    "allow_comment": 1,
    "is_open": 0,
    "city": {
        "id": 13,
        "parent_id": 0,
        "name": "上海市"
    },
    "status": {
        "id": 4,
        "name": "自有冰箱"
    }
},
{
    "id": 32,
    "city_id": 13,
    "title": "四村名",
    "description": "",
    "status_id": 3,
    "latitude": "31.243508",
    "longitude": "121.41411",
    "address": "曹杨四村",
    "content": "",
    "image": null,
    "visible": 1,
    "allow_comment": 1,
    "is_open": 0,
    "city": {
        "id": 13,
        "parent_id": 0,
        "name": "上海市"
    },
    "status": {
        "id": 3,
        "name": "专柜"
    }
},
{
    "id": 33,
    "city_id": 13,
    "title": "八村名",
    "description": "",
    "status_id": 2,
    "latitude": "31.253715",
    "longitude": "121.414496",
    "address": "曹杨八村",
    "content": "",
    "image": null,
    "visible": 1,
    "allow_comment": 1,
    "is_open": 0,
    "city": {
        "id": 13,
        "parent_id": 0,
        "name": "上海市"
    },
    "status": {
        "id": 2,
        "name": "有店"
    }
}]

有这么个数组,需要遍历检索数据,比如想搜索 有店 这个关键字 遍历这个数组,当匹配到当前对象的所有key值有这个关键字的时候就会添加到一个 新的数组里面。

不只是搜索 status.name 这一个 ,也需要模糊匹配 address
要模糊匹配 对象的所有key

这个是叫 全文搜索吗。

阅读 5.2k
3 个回答

推荐用underscore.js

_.filter(data, function(item){ return item.status.name.includes('有店')=== true; });

你只是为了查看当前区域的店面status 不用全部搜索

你这是要在js里实现倒排索引吧。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏