I have an object as follows:
{
"counts": {
"created_by_ref": {
"00f5c303-32c0-4f5f-ac61-bf6577a8f4ed": 1,
"0dde2de1-7951-45b1-8bf3-013f8dbfc4ae": 1
},
"file.hashes.MD5": {
"qqq": 200
},
"ipv4_addr.value": {
"127.0.0.1": 200,
"192.168.1.10": 200
},
"network_traffic.dst_port": {
"xxx": 200
},
"network_traffic.dst_ref.resolves_to_refs.value": {
"yyy": 200
},
"network_traffic.dst_ref.value": {
"rrr": 200
}
}
}
I need to initialize all the values to 0 as follows:
{
"counts": {
"created_by_ref": {
"00f5c303-32c0-4f5f-ac61-bf6577a8f4ed": 0,
"0dde2de1-7951-45b1-8bf3-013f8dbfc4ae": 0
},
"file.hashes.MD5": {
"qqq": 0
},
"ipv4_addr.value": {
"127.0.0.1": 0,
"192.168.1.10": 0
},
"network_traffic.dst_port": {
"xxx": 0
},
"network_traffic.dst_ref.resolves_to_refs.value": {
"yyy": 0
},
"network_traffic.dst_ref.value": {
"rrr": 0
}
}
}
and all the keys except count is unknown and dynamically changing so cannot be hard code. What I did is:
for (const key in getSearchDataAllCounts) {
if (getSearchDataAllCounts.hasOwnProperty(key)) {
Object.keys(getSearchDataAllCounts[key]).forEach((innerkey) => {
getSearchDataAllCounts[key][innerkey] = 0;
});
}
}
But I really think there is a better way doing it in ES6. Is there any improvement I can make to make it more es6 compatible and also do it with one loop preferebly?
for-infor one loop butObject.keys().forEachfor the other...?hasOwnPropertycheck. It's just, it looks like an intentional choice, usingfor-infor the outer loop andObject.keys().forEachfor the inner one. If it's not intentional, no worries, I just thought it might be. :-)