javascript - js json 对象 全文搜索
高洛峰
高洛峰 2017-04-11 12:44:46
[JavaScript讨论组]
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

这个是叫 全文搜索吗。

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(3)
伊谢尔伦

推荐用underscore.js

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

ringa_lee

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

阿神

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号