How To Get Construct Object In Specified Manner From Given Object
Solution 1:
You can use an object lookup for each type of item and property name. Then Using Object.keys()
iterate through each key of lookup and array#reduce
each key with all the values corresponding to lookup property and create an array using array#reduce
of all the values of each object.
var nodeStatus = { "node_stats": { "pendrive": { "mount_status": { "TEST_PROXY": "error", "TEST_TARGET": "error", "TEST_ARCHIVE": "error", "TEST_HOME": "error" } }, "minichip": { "mount_status": { "TEST_PROXY": "na", "TEST_TARGET": "na", "TEST_ARCHIVE":"na", "TEST_HOME": "na" } }, "simcard": { "mount_status":{ "TEST_HOME": "error", "TEST_TARGET": "error", "TEST_ARCHIVE": "error", "TEST_PROXY": "error" } }, "hostname": [ "nikola", "goldplus", "pendrive", "simcard", "airtel", "minichip", "voda" ], "airtel":{ "mount_status": { "TEST_PROXY": "ok", "TEST_TARGET": "ok", "TEST_ARCHIVE": "ok", "TEST_HOME": "ok" } }, "voda": { "mount_status": { "TEST_HOME": "na", "TEST_TARGET": "na", "TEST_ARCHIVE": "na", "TEST_PROXY": "na" } }, "nikola": { "mount_status": { "TEST_HOME":"na", "TEST_ARCHIVE": "na", "TEST_TARGET": "na", "TEST_PROXY": "na" } } } },
lookup = {pendrive: 'TEST_HOME', minichip: 'TEST_PROXY'}
result = Object.keys(lookup).reduce((o,k) => {
o[k] = Object.keys(nodeStatus.node_stats).reduce((r,key) => {
if('mount_status'in nodeStatus.node_stats[key])
r.push(nodeStatus.node_stats[key].mount_status[lookup[k]]);
return r;
},[]);
return o;
},{});
console.log(result);
Solution 2:
If possible i would change the following
const nodeStatus = {
"hostname": [
"nikola",
"goldplus",
"pendrive",
"simcard",
"airtel",
"minichip",
"voda"
],
"node_stats": {
"pendrive": {
"mount_status": {
"TEST_PROXY": "error",
"TEST_TARGET": "error",
"TEST_ARCHIVE": "error",
"TEST_HOME": "error"
}
},
"minichip": {
"mount_status": {
"TEST_PROXY": "na",
"TEST_TARGET": "na",
"TEST_ARCHIVE": "na",
"TEST_HOME": "na"
}
},
"simcard": {
"mount_status": {
"TEST_HOME": "error",
"TEST_TARGET": "error",
"TEST_ARCHIVE": "error",
"TEST_PROXY": "error"
}
},
"airtel": {
"mount_status": {
"TEST_PROXY": "ok",
"TEST_TARGET": "ok",
"TEST_ARCHIVE": "ok",
"TEST_HOME": "ok"
}
},
"voda": {
"mount_status": {
"TEST_HOME": "na",
"TEST_TARGET": "na",
"TEST_ARCHIVE": "na",
"TEST_PROXY": "na"
}
},
"nikola": {
"mount_status": {
"TEST_HOME": "na",
"TEST_ARCHIVE": "na",
"TEST_TARGET": "na",
"TEST_PROXY": "na"
}
}
}
}
const testHome = nodeStatus.hostname.map(name => nodeStatus["node_stats"][name]
? nodeStatus["node_stats"][name]["mount_status"]["TEST_HOME"]
: undefined);
// ["nikola", "goldplus", "pendrive", "simcard", "airtel", "minichip", "voda"]
console.log(testHome) //["na", undefined, "error", "error", "ok", "na", "na"]
hostname differs from all others, so you want to get it out of node_stats
. The order of the result will be the same as the order of hostname
If you don't want the undefined, then you can use Array.reduce
, but the side effect would be that the order of the Array
would be unpredictable.
Solution 3:
If you want output like this:
for key of TEST_ARCHIVE
{error: ["pendrive", "simcard"], na: ["minichip", "voda", "nikola"], ok: ["airtel"]}
Use this:
functiongetNestedValuesForKey(key_name){
var hosts = nodeStatus.node_stats;
var return_values = {};
for( var _hostname in hosts ){
var _host = hosts[_hostname];
if( _host.mount_status ){
var _status = _host.mount_status[key_name];
if( !return_values[_status] ){
return_values[_status] = newArray;
}//if
return_values[_status].push(_hostname);
}//if
}//forreturn return_values
}//functionvar nodeStatus = {"node_stats":{"pendrive":{"mount_status":{"TEST_PROXY":"error","TEST_TARGET":"error","TEST_ARCHIVE":"error","TEST_HOME":"error"}},"minichip":{"mount_status":{"TEST_PROXY":"na","TEST_TARGET":"na","TEST_ARCHIVE":"na","TEST_HOME":"na"}},"simcard":{"mount_status":{"TEST_HOME":"error","TEST_TARGET":"error","TEST_ARCHIVE":"error","TEST_PROXY":"error"}},"hostname":["nikola","goldplus","pendrive","simcard","airtel","minichip","voda"],"airtel":{"mount_status":{"TEST_PROXY":"ok","TEST_TARGET":"ok","TEST_ARCHIVE":"ok","TEST_HOME":"ok"}},"voda":{"mount_status":{"TEST_HOME":"na","TEST_TARGET":"na","TEST_ARCHIVE":"na","TEST_PROXY":"na"}},"nikola":{"mount_status":{"TEST_HOME":"na","TEST_ARCHIVE":"na","TEST_TARGET":"na","TEST_PROXY":"na"}}}};
console.log( getNestedValuesForKey('TEST_ARCHIVE') );
Solution 4:
It's fairly unclear what you want. One possibility is that when you're looking for TEST_PROXY
, you should get
["na", undefined, "error", "error", "ok", "na", "na"]
(The undefined
is because hostname
includes "goldplus", which is not included in node_stats
.)
If that's what you want, then this should work:
const nodeStatus = {"node_stats": {"airtel": {"mount_status": {"TEST_ARCHIVE": "ok", "TEST_HOME": "ok", "TEST_PROXY": "ok", "TEST_TARGET": "ok"}}, "hostname": ["nikola", "goldplus", "pendrive", "simcard", "airtel", "minichip", "voda"], "minichip": {"mount_status": {"TEST_ARCHIVE": "na", "TEST_HOME": "na", "TEST_PROXY": "na", "TEST_TARGET": "na"}}, "nikola": {"mount_status": {"TEST_ARCHIVE": "na", "TEST_HOME": "na", "TEST_PROXY": "na", "TEST_TARGET": "na"}}, "pendrive": {"mount_status": {"TEST_ARCHIVE": "error", "TEST_HOME": "error", "TEST_PROXY": "error", "TEST_TARGET": "error"}}, "simcard": {"mount_status": {"TEST_ARCHIVE": "error", "TEST_HOME": "error", "TEST_PROXY": "error", "TEST_TARGET": "error"}}, "voda": {"mount_status": {"TEST_ARCHIVE": "na", "TEST_HOME": "na", "TEST_PROXY": "na", "TEST_TARGET": "na"}}}}
constnodeSum = (key, nodes) => nodes.node_stats.hostname
.map(hostname => (nodes.node_stats[hostname] || {mount_status: {}}).mount_status[key])
console.log(nodeSum("TEST_PROXY", nodeStatus))
But that's format is fairly unclear. If instead you would like something like this:
{"nikola":"na","pendrive":"error","simcard":"error","airtel":"ok","minichip":"na","voda":"na"}
Then you could modify the function a bit like this:
const nodeStatus = {"node_stats": {"airtel": {"mount_status": {"TEST_ARCHIVE": "ok", "TEST_HOME": "ok", "TEST_PROXY": "ok", "TEST_TARGET": "ok"}}, "hostname": ["nikola", "goldplus", "pendrive", "simcard", "airtel", "minichip", "voda"], "minichip": {"mount_status": {"TEST_ARCHIVE": "na", "TEST_HOME": "na", "TEST_PROXY": "na", "TEST_TARGET": "na"}}, "nikola": {"mount_status": {"TEST_ARCHIVE": "na", "TEST_HOME": "na", "TEST_PROXY": "na", "TEST_TARGET": "na"}}, "pendrive": {"mount_status": {"TEST_ARCHIVE": "error", "TEST_HOME": "error", "TEST_PROXY": "error", "TEST_TARGET": "error"}}, "simcard": {"mount_status": {"TEST_ARCHIVE": "error", "TEST_HOME": "error", "TEST_PROXY": "error", "TEST_TARGET": "error"}}, "voda": {"mount_status": {"TEST_ARCHIVE": "na", "TEST_HOME": "na", "TEST_PROXY": "na", "TEST_TARGET": "na"}}}}
constnodeSum = (key, nodes) => nodes.node_stats.hostname
.map(hostname => ({[hostname]: (nodes.node_stats[hostname] || {mount_status: {}})
.mount_status[key]}))
.reduce((a, b) =>Object.assign(a, b), {})
console.log(nodeSum("TEST_HOME", nodeStatus))
If you could guarantee that there were no scenarios like "goldplus" in your example where a hostname is included in the lists of hostnames but not in node_stats
, then you could simplify these functions to
constnodeSum = (key, nodes) => nodes.node_stats.hostname
.map(hostname => nodes.node_stats[hostname].mount_status[key])
and
constnodeSum = (key, nodes) => nodes.node_stats.hostname
.map(hostname => ({[hostname]: nodes.node_stats[hostname].mount_status[key]}))
.reduce((a, b) =>Object.assign(a, b), {})
And finally, it might be cleaner to do the latter in a single iteration, like this:
constnodeSum = (key, nodes) => nodes.node_stats.hostname
.reduce((acc, hostname) => {
acc[hostname] = nodes.node_stats[hostname].mount_status[key]
return acc
}, {})
nodeSum
is a terrible name for this function, but it seemed that you were updating a variable with this name to hold your output. I would change it to something more explicit for real code.
Post a Comment for "How To Get Construct Object In Specified Manner From Given Object"