web前端全局模糊搜索遇到的坑,求解决方法?

web前端全局模糊搜索遇到的坑
 var arr=[{    //数据
"contact": "",
"content": "工作室原创养成游戏,稿件需求:\n日韩绘风角色Q版形象,头身比约为1/3,色彩鲜亮,无需背景\n用于战斗环境中的角色,需要战斗姿态\n通过约稿后向画师发放角色设定,根据设定制作,分别有人形角色或者怪物形角色\n绘制过程尽量分层以方便后期制作小规模的特效",
"id": 1,
"picurl": "http://pic.afunpc.com/img/yuku/detail1.jpg",
"picurl2": "",
"picurl3": "",
"sub_type": 1,
"title": "Q版角色",
"type": 1
}, {
"contact": "",
"content": "条漫上色,偏日系风格少年漫上色风格,要求上色熟练,上色仔细,色感好,责任心强,不拖稿。",
"id": 2,
"picurl": "http://pic.afunpc.com/img/yuku/hort2.jpg",
"picurl2": "",
"picurl3": "",
"sub_type": 1,
"title": "条漫上色",
"type": 1
}, {
"contact": "",
"content": "有人设。需要人物跟背景分离。\n赛璐璐,平涂。\n用户展会海报及相关门票宣传物料画面。\n背景需画大点之后需要横版作为门票画面。",
"id": 3,
"picurl": "http://pic.afunpc.com/img/yuku/indexTab3.jpg",
"picurl2": "",
"picurl3": "",
"sub_type": 1,
"title": "漫展海报",
"type": 1
}, {
"contact": "",
"content": "用作日式和风二次元麻将游戏的主界面背景图,场景的内容可以参考例图。\n主要参考内容:\n1.从和室内向外看的视角\n2.夜景\n3.屋外灯火通明\n4.整体调子、氛围\n主要设计的内容:\n1.融入和风、富贵、麻将相关的元素\n2.别太像古代魔幻(阴阳师),世界观设定还是现代的,在传统的建筑里休闲打麻将\n3.需要和二次元比较搭的绘画风格,如新海诚的动画场景等。",
"id": 4,
"picurl": "http://pic.afunpc.com/img/yuku/indexTab2.jpg",
"picurl2": "",
"picurl3": "",
"sub_type": 1,
"title": "日式和室背景图",
"type": 1
}, {
"contact": "",
"content": "vv",
"id": 13,
"picurl": "cc",
"picurl2": "",
"picurl3": "",
"sub_type": 1,
"title": "abc",
"type": 1
}, {
"contact": "",
"content": "",
"id": 21,
"picurl": "",
"picurl2": "",
"picurl3": "",
"sub_type": 0,
"title": "",
"type": 1
}, {
"contact": "",
"content": "",
"id": 22,
"picurl": "",
"picurl2": "",
"picurl3": "",
"sub_type": 0,
"title": "",
"type": 1
}, {
"contact": "",
"content": "",
"id": 23,
"picurl": "",
"picurl2": "",
"picurl3": "",
"sub_type": 0,
"title": "",
"type": 1
}, {
"contact": "",
"content": "",
"id": 24,
"picurl": "",
"picurl2": "",
"picurl3": "",
"sub_type": 0,
"title": "",
"type": 1
}, {
"contact": "",
"content": "",
"id": 25,
"picurl": "",
"picurl2": "",
"picurl3": "",
"sub_type": 0,
"title": "",
"type": 1
}, {
"contact": "",
"content": "",
"id": 26,
"picurl": "",
"picurl2": "",
"picurl3": "",
"sub_type": 0,
"title": "",
"type": 1
}, {
"contact": "1856661348561",
"content": "测试发布一测试发布一",
"id": 40,
"picurl": "http://pic.afunpc.xyz/image/2018/1524585600/i1524621835-58358.png",
"picurl2": "",
"picurl3": "",
"sub_type": 131456,
"title": "测试发布一",
"type": 1
}, {
"contact": "13823370696",
"content": "z上的",
"id": 41,
"picurl": "",
"picurl2": "",
"picurl3": "",
"sub_type": 196992,
"title": "bvxcvdfg",
"type": 1
}, {
"contact": "13823370696",
"content": "z??????",
"id": 42,
"picurl": "",
"picurl2": "",
"picurl3": "",
"sub_type": 196992,
"title": "bvxcvdfg",
"type": 1
}, {
"contact": "13823370696",
"content": "发布测试",
"id": 43,
"picurl": "http://pic.afunpc.xyz/image/2018/1524585600/i1524622577-57618.png",
"picurl2": "",
"picurl3": "",
"sub_type": 196992,
"title": "测试",
"type": 1
}, {
"contact": "13823370696",
"content": "???????",
"id": 44,
"picurl": "http://pic.afunpc.xyz/image/2018/1524585600/i1524622577-57618.png",
"picurl2": "",
"picurl3": "",
"sub_type": 196992,
"title": "?",
"type": 1
}, {
"contact": "",
"content": "b",
"id": 45,
"picurl": "",
"picurl2": "",
"picurl3": "",
"sub_type": 2,
"title": "v",
"type": 1
}, {
"contact": "sdrfe",
"content": "b",
"id": 46,
"picurl": "c",
"picurl2": "c",
"picurl3": "c",
"sub_type": 1,
"title": "a",
"type": 1
}]

//keyword 是获取用户输入的值
function vague(keyword){
return data.filter(function (obj) {
for(let key in obj){
let str=obj[key]
return str.toString().indexOf(keyword)!=-1
}
});
}
咋看好像没问题,但是如果有字段是空字符串的话,str.toString().indexOf(keyword)!=-1 是false
看着就心塞,完全不知道怎么做了,也不知那里错了,求解。


然后我又想了另外一种方法,直把obj变成字符串算了,虽然达到了要求,但是字段名也列入模糊搜索的范围了。
function vague2(keyword){
return data.filter(function (obj) {
var str=JSON.stringify(obj);
return str.toString().indexOf(keyword)!=-1
});

}


最后求大神赐予我一个符合需求的解法,为什么遇到空字符传就不能查询了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值