This is challenging task for me. I have an array of object. I need to loop thru an array , if there is "message" key, add it to a new array. But there is a tricky thing here. If 'render' is true , I need to get "stageID"(for instance in fisrt element stadeID = 3 ) and move to the third element in that array, get 'message' if there is one , again add it to array and check for "render" status,keep looping until render is false. If in element no 'message' key , but render is true , anyway I need to move to elements by it's stageID.
P/S I always need to get message from first element , doesn't matter if render is true or not
[
{
"algID": 0,
"render": true,
"message": "first message",
"stageID": "3"
},
{
"algID": 0,
"render": false,
"message": "second message",
"message_type": "text"
},
{
"algID": 0,
"render": true,
"message": "third message",
"message_type": "text",
"stageID": "5"
},
{
"algID": 0,
"render": false
},
{
"algID": 0,
"render": false,
"stageID": "4"
},
{
"algID": 0,
"render": false
}
]
Here is my function. I'm adding first element 'message' to array , checking for 'render' but I have no idea how loop it moving thru array using 'stageID'
displayData(step) {
let arrayWithMessage=[]
for (let z = 0; z < step.length; z++) {
if ("message" in step[z]) {
arrayWithMessage.push(step[z].message)
}
if (step[z].render === true) {
console.log("reder true", step[step[z].stageID - 1])
}
}
}
expected output: ['first message', 'third message']
"second message"?