用过滤器获取对象的数据

问题描述

大家好,我对Java语言中的对象有疑问。 好的,我有这个结构

我的对象

{
  "client": {
    "rpc2": {
      "testEstrobos": false
    },"rpc1": {
      "testParlantes": false
    },"channelServerId": 0,"channelClientStatus": false,"testParlantes": false
  },"shared": {
    "atributoServidor": "modificadoporTenant"
  }
}

我想获得{ rpc1: { testEstrobos: false},rpc2: { testParlantes: false } },但只有一个条件。 JSON可以更新,因为最后它是通过API获得的。我将需要某种类型的过滤器,例如función,就像我仅获得RPC时一样。我使用了功能 match indexOf 。最后一个过滤器可以过滤,但是只会给我一个没有结构的名字

解决方法

有点棘手。首先获取您的客户端对象的密钥,并在所有{key1以'rpc'开头的属性中获取filter。然后与foreach讨论密钥。对于每个键,将一个具有此键名及其属性的新属性添加到新对象。

let obj = {
  "client": {
    "rpc2": {
      "testEstrobos": false
    },"rpc1": {
      "testParlantes": false
    },"channelServerId": 0,"channelClientStatus": false,"testParlantes": false
  },"shared": {
    "atributoServidor": "modificadoporTenant"
  }
};

let res = {};
Object.keys(obj.client).filter(key => key.substr(0,3)==='rpc').forEach(key => res[key] = obj.client[key]);
console.log(res);