I am new to JavaScript and am looking for a way to: first, extract values from a desired key in a complex JSON object. Second, if the value does not equal a given string, print the parent key. Here is a simplified JSON object that needs to be parsed. There are many more entries in the file.
{
"Test1": {
"protocolName": "Test1",
"createdAsProtocolName": "AnalyticsTest1",
"message": "Protocol already exists!",
"importStatus": "success",
"protocolApplicationName": "Flexi-Protocol",
"protocolId": 1,
"applicationId": 5
},
"Test2": {
"protocolName": "Test2",
"createdAsProtocolName": "AnalyticsTest2",
"message": "Protocol already exists!",
"importStatus": "success",
"protocolApplicationName": "Flexi-Protocol",
"protocolId": 2,
"applicationId": 5
},
"Test3": {
"protocolName": "Test3",
"createdAsProtocolName": "AnalyticsTest3",
"message": "Error",
"importStatus": "failed",
"protocolApplicationName": "Flexi-Protocol",
"protocolId": 3,
"applicationId": 5
},
"Test4": {
"protocolName": "Test4",
"createdAsProtocolName": "AnalyticsTest4",
"message": "Error",
"importStatus": "failed",
"protocolApplicationName": "Flexi-Protocol",
"protocolId": 4,
"applicationId": 5
}
}
I want to check each test's ['importstatus'] for "success" and if any say otherwise, save them in an array ["Test3", "Test4"]
CodePudding user response:
For each key/value pair in yourObject, filter out any with importStatus of success, and return the keys of the rest.
const arrayOfFailures = Object.entries(yourObject)
.filter(([k, v]) => v.importStatus !== "success")
.map(([k, v]) => k);