The problem of string array replacement step by step

The goal of

is to replace the null string in each element with the next element, resulting in a replacement string.

related codes

/ / the list array is as follows

var list = [
  "data : {null}", 
  "item : ( payload.data.list map(value0, index0) -> {null})",
  "gradeList : ( value0.gradeList map(value1, index1) -> {null})", 
  "otherList : ( value1.otherList map(value2, index2) -> {null})", 
  "anotherList : ( value2.anotherList map(value3, index3) -> {null})"
]

the desired result is a string

 "data : {
    item : ( payload.data.list map(value0, index0) -> {
        gradeList : ( value0.gradeList map(value1, index1) -> {
          otherList : ( value1.otherList map(value2, index2) -> {
            anotherList : ( value2.anotherList map(value3, index3) -> {null})
          })
        })
    })
  }"
Jun.24,2022


for (let i = list.length - 2; i >= 0; i--) {
  list[i] = list[i].replace(/{\s*null\s*}/, `{\n ${"\t".repeat(i)}${list[i + 1]} }`);
}

for (var i = list.length-1,str=''; i >= 0; i--) {
  (obj = list[i].replace(/null/,str),str = obj);
}
console.log(str);
Menu