Reputation: 15589
I have an Object
obj
as below:
const obj = {
Hostname: "abc.com"
Check1Status: "PASS"
Check2Status: "PASS"
Check3Status: "FAIL"
Check3ErrorHeading: "Reason for Check3"
Check3ErrorDetail: "Details for Check3"
Check4Status: "FAIL"
Check4ErrorHeading: "Reason for Check4"
Check4ErrorDetail: "Details for Check4"
TotalExecutions: 5
};
TotalExecutions
PASS
as the value.Hostname
and it value.checks
which have Status
as FAIL
such that the output looks as below:
TotalExecutions:5,Check3:Reason for Check3:Details for Check3, Check4:Reason for Check4:Details for Check4
The final output should be a string
.
I have written the following code which is excluding PASS
and including TotalExecutions
and excluding HostName
but not implementing 4
const result = Object.values(obj).map
(el => Object.entries(el).filter(([key, value]) => value !== 'PASS' && key !== 'Hostname').reduce((acc, [key, value]) => ({
...acc,
[key]: value
}), {}));
Upvotes: 0
Views: 53
Reputation: 13
Hope it can help you
function formatter() {
const obj = {
Hostname: "abc.com",
Check1Status: "PASS",
Check2Status: "PASS",
Check3Status: "FAIL",
Check3ErrorHeading: "Reason for Check3",
Check3ErrorDetail: "Details for Check3",
Check4Status: "FAIL",
Check4ErrorHeading: "Reason for Check4",
Check4ErrorDetail: "Details for Check4",
TotalExecutions: 5
};
let result = `TotalExecutions:${obj['TotalExecutions']}`;
Object.keys(obj).forEach(key => {
if (obj[key] === 'FAIL') {
const checkKey = key.match(/Check\d/);
const errorHeading = obj[`${checkKey}ErrorHeading`];
const errorDetail = obj[`${checkKey}ErrorDetail`];
result += `,${checkKey}:${errorHeading}:${errorDetail}`;
}
});
return result;
}
console.log(formatter());
Upvotes: 0
Reputation: 6996
const obj = {
Hostname: "abc.com",
Check1Status: "PASS",
Check2Status: "PASS",
Check3Status: "FAIL",
Check3ErrorHeading: "Reason for Check3",
Check3ErrorDetail: "Details for Check3",
Check4Status: "FAIL",
Check4ErrorHeading: "Reason for Check4",
Check4ErrorDetail: "Details for Check4",
TotalExecutions: 5
};
var output = [`Total Executions: ${obj.TotalExecutions}`];
for (var key in obj) {
if(obj[key] === 'FAIL') {
output.push(`
${key.replace('Status', '')}:
${obj[key.replace('Status', 'ErrorHeading')]}:
${obj[key.replace('Status', 'ErrorDetail')]}`)
}
}
console.log(output.join(', '))
Upvotes: 1
Reputation: 864
The following should work for You.
const obj = {
Hostname: "abc.com",
Check1Status: "PASS",
Check2Status: "PASS",
Check3Status: "FAIL",
Check3ErrorHeading: "Reason for Check3",
Check3ErrorDetail: "Details for Check3",
Check4Status: "FAIL",
Check4ErrorHeading: "Reason for Check4",
Check4ErrorDetail: "Details for Check4",
TotalExecutions: 5
};
let resultObj = {};
for (let prop in obj) {
if (prop == "TotalExecutions") {
resultObj[prop] = obj[prop];
}
if (obj[prop] == "FAIL") {
let startOfKey = prop.replace("Status", "");
resultObj[startOfKey] = obj[startOfKey + "ErrorHeading"] + ":" + obj[startOfKey + "ErrorDetail"]
}
}
console.log(resultObj)
Upvotes: 1