Update an array within an object array

Options
Hello,

I have a "comments" object array within an object array of "Posts".

I want to selectively  push this comments array into a fresh results object array that is similar to posts, but i am unable to traverse the results object array in the for loop.

Posts:
{
user
{
username: "ABC",
...,
}
Comments:
{
Comment id= 124,
Parent_id=0
}
}

I am basically looking to select all comments within each post to select and push all parent comments into a new results object


1. So created a for loop for each of the Posts as item

2. created results object variable and updated with everything else including a "comments" path for comments[] (blank array).

3. Then have a for loop for all comments in item as "commentitem"
3.a. Within this loop a if conditional to check if the loop has parentid = 0
Issue: now i need to push the identified commentitem into results object for that post, and i am unable to traverse to results.0.comments[] and results.1.comments[]

how do i assign or push commentitem[] into results.0 or results.0.comments[] and so on?

I am using a variable (i) to count the post items i have already gone through in loop 1 and tried using that in 3a e.g. results[i].comments - unfortunately this is not possible to be used.

Comments