Conrad Klek
Conrad Klek

Reputation: 143

Is there an easier, recursive way to write this function?

I have a javascript function that accepts an array and performs another function on each item of that array. There are a lot of repeating parts, so I'm assuming there is a simpler recursive way to write this:

function tree(life) {
  for (let bugs of life) {
    if (typeof bugs === `object`) {
      for (let soil of bugs) {
        if (typeof soil === `object`) {
          for (let dirt of soil) {
            if (typeof dirt === `object`) {
              for (let stem of dirt) {
                if (typeof stem === `object`) {
                  for (let leaf of stem) {
                    if (typeof leaf === `object`) {
                      for (let weed of leaf) {
                        if (typeof weed === `object`) {
                          for (let puff of weed) {
                            if (typeof puff === `object`) {
                              for (let high of puff) {
                                if (typeof high === `object`) {
                                  for (let toke of high) {
                                    if (typeof toke === `object`) {} else {
                                      $tree.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.appendChild(dom(toke))
                                    }
                                  }
                                } else {
                                  $tree.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.appendChild(dom(high))
                                }
                              }
                            } else {
                              $tree.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.appendChild(dom(puff))
                            }
                          }
                        } else {
                          $tree.lastElementChild.lastElementChild.lastElementChild.lastElementChild.lastElementChild.appendChild(dom(weed))
                        }
                      }
                    } else {
                      $tree.lastElementChild.lastElementChild.lastElementChild.lastElementChild.appendChild(dom(leaf))
                    }
                  }
                } else {
                  $tree.lastElementChild.lastElementChild.lastElementChild.appendChild(dom(stem))
                }
              }
            } else {
              $tree.lastElementChild.lastElementChild.appendChild(dom(dirt))
            }
          }
        } else {
          $tree.lastElementChild.appendChild(dom(soil))
        }
      }
    } else {
      $tree.appendChild(dom(bugs));
    }
  }
}

Each item in the array is either going to be a string or a nested array. The strings don't have to be unique so I don't think I can use an object and map() instead.

Upvotes: 0

Views: 55

Answers (1)

stackoverfloweth
stackoverfloweth

Reputation: 6917

pseudo code, something like this

function traverse(target, parent){
   if(typeof target === `string`){
      parent.appendChild(dom(target));
   }else{
      target.forEach(node => traverse(node, parent.lastElementChild)
   }
}

traverse(life, $tree)

Upvotes: 4

Related Questions