Have an object shown below were I need to iterate over each object property to find nextStep and push to an array. My output should have a single array variable with 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 tried below code, but it returns null due to scoping issue:
function iterObj(obj) {
var result = [];
for (var key in obj) {
if (
obj[key] !== null &&
typeof obj[key] === "object" &&
key == "nextStep"
) {
var data = this.iterObj(obj[key]);
result.push(data);
}
}
return result;
}
iterObj(obj);