Looking for a way to loop through each property of an object shown below in order to find the "nextStep" and add it to an array. The desired output is to have a single array variable containing all "nextStep" properties.
Input:
{
"Product1": {
"stepName": "step1",
"stepOutputStatus": "normal",
"nextStep": {
"stepName": "step2",
"stepOutputStatus": "normal",
"nextStep": {
"stepName": "step3",
"stepOutputStatus": "warning",
"nextStep": {
"stepName": "step4",
"stepOutputStatus": "warning",
"nextStep": null
}
}
}
}
}
Expected Output:
[
{
"stepName": "step2",
"stepOutputStatus": "normal"
},
{
"stepName": "step3",
"stepOutputStatus": "warning"
},
{
"stepName": "step4",
"stepOutputStatus": "warning"
}
]
I attempted the following code, but it returns null due to scoping issue:
function iterateObject(obj) {
var result = [];
for (var key in obj) {
if (
obj[key] !== null &&
typeof obj[key] === "object" &&
key == "nextStep"
) {
var data = this.iterateObject(obj[key]);
result.push(data);
}
}
return result;
}
iterateObject(obj);