algorithm for list display I'm working on a website which gets a list of objects like this:

Object { model: "modelhandler.comment", pk: 5, fields: Object } 

where the field objects contains this:

Object { user: "TestUser",id: 6, video: 7, description: "test new video comment lol", reply_to: 5, uploaded: "2018-01-12T09:14:24.281Z", updated: "2018-01-12T09:14:24.281Z", indent: 0}

The goal of the site is to find all comments which have reply_to set to a number (not null) and increment it's indent value and all it's children's indent value.

for example consider this list of comments:

comment 1 (id: 1, indent: 0, reply_to: null)
comment 2 (id: 2, indent: 0, reply_to: null)
comment 3 (id: 3, indent: 0, reply_to: 1)
comment 4 (id: 4, indent: 0, reply_to: 3)
comment 5 (id: 5, indent: 0, reply_to: 1)
comment 6 (id: 6, indent: 0, reply_to: null)


Now I want to run trough the list and set all indent values according to the reply_to value. so the list becomes:

comment 1 (id: 1, indent: 0, reply_to: null)
comment 2 (id: 2, indent: 0, reply_to: null)
comment 3 (id: 3, indent: 1, reply_to: 1)
comment 4 (id: 4, indent: 2, reply_to: 3)
comment 5 (id: 5, indent: 1, reply_to: 1)
comment 6 (id: 6, indent: 0, reply_to: null)


Now this has to happen inside a for loop which allready runs trough the list once:

for(var i = 0; i &lt; array.length; i++) {
    //do stuff here
    //set the indent value here 
    if(reply_to)
        indent++;

    //do more stuff here
}


this was my idea for achieving this but it will only increment indent once as it travels trough the list only once. is there a better way to do this?
Assuming the replies are guaranteed to be below the lines they're replying to, you can do this.

if (reply_to != 0)
{
    array[i].indent = array[reply_to].indent + 1;
}