我有一个从vip生成的json数据,并且有很多字段,我想过滤掉那些只需要的字段的数据。像这样的人:
mycontacts=
[
{
"owner": "swapneil",
"fleet_name": "RancorService",
"creation_date": "09-03-2020",
"environment": "RancorService/JP",
"vip_port": 80,
"partition": "YES",
"protocol": "HTTP",
"monitor": "Consistent (http-ping)",
"lbset": "Consistent",
"predictor": "Consistent",
"spillover": "Consistent",
"vip_cka": "Consistent :(NO)",
"max_conns": "Consistent : (36)",
"vip_cipher": null,
"vip_cip": "Consistent",
"vip_tier": "Consistent(Tier1)",
"vip_siloed": "Consistent(Non-SILOED)",
"vip_globalized": "Consistent(Non-GLOBALIZED)",
"vip_dedicated": "Consistent(Non-DEDICATED_VIP)",
"vip_retail_tag": "Consistent(RETAIL_TAG)",
"down_stat_flush": "Consistent :(ENABLED)",
"vip_type": "Consistent (BACKEND)",
"server_port": "Consistent",
"vip_server_timeout": "Consistent",
"vip_client_timeout": "Consistent",
"persistent_method": "Consistent",
"persistent_timeout": "Consistent"
},
{
"owner": "swapneil",
"fleet_name": "RancorService",
"creation_date": "09-03-2020",
"environment": "RancorService/JP",
"vip_port": 443,
"partition": "YES",
"protocol": "TCP",
"monitor": "Consistent (ssl-ping)",
"lbset": "NC (>1 Vip's in same LB)",
"predictor": "NC (leastconns,ROUNDROBINSHOULD BE ROUNDROBIN)",
"spillover": "Consistent",
"vip_cka": "Consistent :(NO)",
"max_conns": "Consistent : (9000)",
"vip_cipher": null,
"vip_cip": "Consistent",
"vip_tier": "Consistent(Non-Tier1)",
"vip_siloed": "Consistent(Non-SILOED)",
"vip_globalized": "Consistent(Non-GLOBALIZED)",
"vip_dedicated": "Consistent(Non-DEDICATED_VIP)",
"vip_retail_tag": "Consistent(RETAIL_TAG)",
"down_stat_flush": "Consistent :(ENABLED)",
"vip_type": "Consistent (BACKEND)",
"server_port": "Consistent",
"vip_server_timeout": "Consistent",
"vip_client_timeout": "Consistent",
"persistent_method": "Consistent",
"persistent_timeout": "Consistent"
}
]我想从这个json中筛选出这些字段:结果应该是json只有下面提到的那些字段和值。
{
"fleet_name",
"environment",
"vip_port",
"protocol",
"lbset",
"predictor",
"spillover",
"vip_cipher",
"vip_cip",
"vip_tier",
"vip_siloed",
"vip_globalized",
"vip_dedicated",
"vip_retail_tag",
"down_stat_flush",
"vip_type",
"vip_client_timeout"
}如何在javascript中做到这一点?
我只想要上面提到的那些字段的数据,剩下的留给我。
发布于 2020-04-15 18:17:08
这个怎么样?
var src = [{"owner": "swapneil","fleet_name": "RancorService","creation_date": "09-03-2020","environment": "RancorService/JP","vip_port": 80,"partition": "YES","protocol": "HTTP","monitor": "Consistent (http-ping)","lbset": "Consistent","predictor": "Consistent","spillover": "Consistent","vip_cka": "Consistent :(NO)","max_conns": "Consistent : (36)","vip_cipher": null,"vip_cip": "Consistent","vip_tier": "Consistent(Tier1)","vip_siloed": "Consistent(Non-SILOED)","vip_globalized": "Consistent(Non-GLOBALIZED)","vip_dedicated": "Consistent(Non-DEDICATED_VIP)","vip_retail_tag": "Consistent(RETAIL_TAG)","down_stat_flush": "Consistent :(ENABLED)","vip_type": "Consistent (BACKEND)","server_port": "Consistent","vip_server_timeout": "Consistent","vip_client_timeout": "Consistent","persistent_method": "Consistent","persistent_timeout": "Consistent"},{"owner": "swapneil","fleet_name": "RancorService","creation_date": "09-03-2020","environment": "RancorService/JP","vip_port": 443,"partition": "YES","protocol": "TCP","monitor": "Consistent (ssl-ping)","lbset": "NC (>1 Vip's in same LB)","predictor": "NC (leastconns,ROUNDROBINSHOULD BE ROUNDROBIN)","spillover": "Consistent","vip_cka": "Consistent :(NO)","max_conns": "Consistent : (9000)","vip_cipher": null,"vip_cip": "Consistent","vip_tier": "Consistent(Non-Tier1)","vip_siloed": "Consistent(Non-SILOED)","vip_globalized": "Consistent(Non-GLOBALIZED)","vip_dedicated": "Consistent(Non-DEDICATED_VIP)","vip_retail_tag": "Consistent(RETAIL_TAG)","down_stat_flush": "Consistent :(ENABLED)","vip_type": "Consistent (BACKEND)","server_port": "Consistent","vip_server_timeout": "Consistent","vip_client_timeout": "Consistent","persistent_method": "Consistent","persistent_timeout": "Consistent"}];
var fields = ["fleet_name","environment","vip_port","protocol","lbset","predictor","spillover","vip_cipher","vip_cip","vip_tier","vip_siloed","vip_globalized","vip_dedicated","vip_retail_tag","down_stat_flush","vip_type","vip_client_timeout"];
var res = [];
for(var l1 in src) {
var res2 = {};
for(var l2 in src[l1]) {
if (fields.indexOf(l2) > -1) {
res2[l2] = src[l1][l2];
}
}
res.push(res2);
}
console.log(JSON.stringify(res,null,1));
https://stackoverflow.com/questions/61231456
复制相似问题