请教一个json 多级过滤的问题

gggso
  • 170

项目中用到了一个2层json,现在有一个搜索功能,根据第二层json中 username 字段来进行搜索过滤,就是一个搜索功能,如果第二层有匹配数据就显示第一层第二层数据,没有就第一层都不显示。我想用 2层 filter 直接过滤出来,但是有些转不过来弯,求指教
以下是数据结构截图和代码

clipboard.png

{"64":{"groupid":64,"groupname":"李伟","online":0,"open":false,"select":false,"list":{"64-64":{"username":"李伟","id":"64-64","avatar":"/static/img/avatar/no_avatar.png","sign":"wxid_n7fayv381dxh22","online":1,"status":"","unread":0,"fromuid":"64","fromuname":"李伟","select":false},"64-62":{"username":"NcHPmzHfGY","id":"64-62","avatar":"/static/img/avatar/no_avatar.png","sign":"wxid_8wink999rszm22","online":1,"status":"","unread":0,"fromuid":"64","fromuname":"李伟","select":false},"64-60":{"username":"团队","id":"64-60","avatar":"http://wx.qlogo.cn/mmhead/cypR72jV8BHjDwNh3Nc1YcsgzmiaZacpR1dgiaibt4QuMs/0","sign":"weixin","online":1,"status":"","unread":0,"fromuid":"64","fromuname":"李伟","select":false},"64-57":{"username":"bot yang","id":"64-57","avatar":"http://wx.qlogo.cn/mmhead/LIMVN2XWG8GkwyicDMpc44eKPl9kaUiawJyaq1Qe1Iaiby76cSqhDSnyw/0","sign":"wxid_nc629a7n1dvp22","online":1,"status":"","unread":0,"fromuid":"64","fromuname":"李伟","select":false},"64-288":{"username":"眯眯","id":"64-288","avatar":"http://wx.qlogo.cn/mmhead/ver_1/OG86Z9E5KU5SBrIvSNJjibQZhn1VK9kbbhBhpsvhME0w9ibusib90PanicHFLYP5JQnia2JgDYOkKfaMUTBV3cWmqgw/0","sign":"yangshuhai","online":1,"status":"","unread":0,"fromuid":"64","fromuname":"李伟","select":false}}},"65":{"groupid":65,"groupname":"Ava","online":0,"open":false,"select":false,"list":{"65-65":{"username":"Ava","id":"65-65","avatar":"http://127.0.0.1:8184/api/log/file?filekey=e7bae8eecefbcde6169d2dbe55affbb2","sign":"wxid_cdznyt4gqzro22","online":1,"status":"","unread":0,"fromuid":"65","fromuname":"Ava","select":false},"65-66":{"username":"-秋天的红叶","id":"65-66","avatar":"http://wx.qlogo.cn/mmhead/ver_1/udIfAQibTnGiceL3O7q1lIdf9vUobKSdjRIPg6qOiaoO1DgiaOAbYz104skSciaL9tmJB1UGqZzfaQc6IYw1JE5uROKpxxUSRibibPtZayCrmCjakI/0","sign":"wxid_5jo6u4jqrc0q22","online":1,"status":"","unread":0,"fromuid":"65","fromuname":"Ava","select":false},"65-62":{"username":"NcHPmzHfGY","id":"65-62","avatar":"/static/img/avatar/no_avatar.png","sign":"wxid_8wink999rszm22","online":1,"status":"","unread":0,"fromuid":"65","fromuname":"Ava","select":false},"65-60":{"username":"团队","id":"65-60","avatar":"http://wx.qlogo.cn/mmhead/cypR72jV8BHjDwNh3Nc1YcsgzmiaZacpR1dgiaibt4QuMs/0","sign":"weixin","online":1,"status":"","unread":0,"fromuid":"65","fromuname":"Ava","select":false},"65-57":{"username":"bot yang","id":"65-57","avatar":"http://wx.qlogo.cn/mmhead/LIMVN2XWG8GkwyicDMpc44eKPl9kaUiawJyaq1Qe1Iaiby76cSqhDSnyw/0","sign":"wxid_nc629a7n1dvp22","online":1,"status":"","unread":0,"fromuid":"65","fromuname":"Ava","select":false},"65-288":{"username":"瞄了个眯眯","id":"65-288","avatar":"http://wx.qlogo.cn/mmhead/ver_1/OG86Z9E5KU5SBrIvSNJjibQZhn1VK9kbbhBhpsvhME0w9ibusib90PanicHFLYP5JQnia2JgDYOkKfaMUTBV3cWmqgw/0","sign":"yangshuhai","online":1,"status":"","unread":0,"fromuid":"65","fromuname":"Ava","select":false},"65-289":{"username":"Massage","id":"65-289","avatar":"/static/img/avatar/no_avatar.png","sign":"12312124396@chatroom","online":1,"status":"","unread":0,"fromuid":"65","fromuname":"Ava","select":false}}}}
回复
阅读 1.2k
2 个回答

data为你的数据,keyword为搜索关键字

var ret = {}
Object.keys(data).forEach(key => {
  var names = Object.keys(data[key].list).filter(name => {
    let _item = data[key].list[name]
    return _item.username === keyword
  })
  if (names.length > 0) {
    let obj  =ret[key] = Object.assign({}, data[key])
    obj.list = {}
    names.forEach(name => {
      obj.list[name] = data[key].list[name]
    })
  }
})
console.log(ret)

仔细看了下,json是对象,不能用filter,难道只能用 for 去循环吗。

你知道吗?

宣传栏