大家好,我有一个关于 Javascript 对象的问题。好的,我有这个结构
我的对象
{
"client": {
"rpc2": {
"testEstrobos": false
},
"rpc1": {
"testParlantes": false
},
"channelServerId": 0,
"channelClientStatus": false,
"testParlantes": false
},
"shared": {
"atributoServidor": "modificadoporTenant"
}
}
我想获得{ rpc1: { testEstrobos: false}, rpc2: { testParlantes: false } }
但只有一个条件。JSON 可以更新,因为它最终是由 API 获取的。如果我只获得 RPC,我将需要某种类型的过滤器,例如 SQL 中的 función Like。我使用了函数match和indexOf。最后一个确实过滤,但只会给我没有结构的名字
这有点棘手。首先获取您的客户端对象的键,并filter
针对所有键名以“rpc”开头的属性。然后通过键与 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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句