Search code examples
jsonecmascript-6ecmascript-5javassist

How to get the key and values of a json string based on a string in the key


I have a JSON file that look like the following:

{
"rooms":{
  "operatorLimbo":[
     
  ],
  "pager":[
     "w0vu9ourxkdpkf0vwosrim",
     "mekkgn9e9kdpwf2rzpkf41",
     "upkicevngkd5s2h8o01wyz",
     "x29ywtag4kfs0mylq2rd4v"
  ],
  "ru_pagers":[
     "mekkgn9e9kdpwf2rzpkf41"
  ],
  "ch_pagers":[
     "upkicevngkd5s2h8o01wyz"
  ],
  "operators":[
     "r9agylhvekcoibhrk8db8c",
     "p94jb2ocyk5hv0ch8tivk5",
     "v7cxhqxa3kdgik1h4t1sx0",
     "spuvkm9nnketwnqeelqzbf"
  ],
  "pl_operators":[
     "spuvkm9nnketwnqeelqzbf"
  ],
  "clients":[
     "g99g943hlkh0kp1864rwpz",
     "prky4witjkh0naaswiewzs"
  ],
  "pl_users":[
     
  ],
  "ru_operators":[
     "r9agylhvekcoibhrk8db8c"
  ],
  "ch_operators":[
     "v7cxhqxa3kdgik1h4t1sx0"
  ],
  "nl_operators":[
     "p94jb2ocyk5hv0ch8tivk5"
  ],
  "nl_pagers":[
     "w0vu9ourxkdpkf0vwosrim"
  ],
  "ch_users":[
     "prky4witjkh0naaswiewzs"
  ],
  "nl_users":[
     "g99g943hlkh0kp1864rwpz"
  ],
  "pl_pagers":[
     "x29ywtag4kfs0mylq2rd4v"
  ],
  "ru_users":[
     
  ],
  "us_operators":[
     
  ],
  "au_operators":[
     
  ],
  "au_users":[
     
  ],
  "au_pagers":[
     
  ],
  "za_operators":[
     
  ],
  "yoiqfvvvokh0kp2v53sic9":{
     "roomid":"yoiqfvvvokh0kp2v53sic9",
     "type":"1to1",
     "ujid":"g99g943hlkh0kp1864rwpz",
     "aid":"3173aaacd43941c7bef1a99e0057ba2b",
     "operator":{
        "fullname":"Operator",
        "name":"Operator",
        "lastname":"Jura"
     },
     "members":[
        "Ss0G3RCClvOO8I03AAIp",
        "jZGZ_yAb-SC3bEpmAAJz"
     ],
     "chatRecord":11151,
     "ojid":"p94jb2ocyk5hv0ch8tivk5",
     "usid":"jZGZ_yAb-SC3bEpmAAJz"
  },
  "deomjtr27kh0l52ogg9zxh":{
     "roomid":"deomjtr27kh0l52ogg9zxh",
     "type":"1to1",
     "ujid":"prky4witjkh0naaswiewzs",
     "aid":"3173aaacd43941c7bef1a99e0057ba2b",
     "operator":{
        "fullname":"Operator",
        "name":"Operator",
        "lastname":"Jura"
     },
     "members":[
        "DPeOVb1P6qCVXO6GAAJi",
        "AAkb84mpMxLzFHklAAJ7"
     ],
     "chatRecord":11153,
     "ojid":"v7cxhqxa3kdgik1h4t1sx0",
     "usid":"AAkb84mpMxLzFHklAAJ7"
  }
  }
}

What I'm interested in is to get values like ch_operators and pl_operators and so on basically all entries that have _operators in it and also the value of it,

with the following code I can get just the keys, how can I get the values as well,

Object.keys(this.rooms).filter((key) => key.includes("_operators"))

any help would be appreciated.


Solution

  • You can use a combination of Object.entries, .filter and Object.fromEntries to create a new object with selected properties:

    Object.fromEntries(
      Object.entries(this.rooms).filter(([key]) => key.includes("_operators"))
    )
    

    Object.entries returns an array containing both key and value (as "tuple"), and Object.fromEntries builds a new object from such an array.