I have a general function that I want to call with any object structure. Some of the objects has arrays of objects which then again have arrays of objects etc.
The only way I got it to work was like this and it does not seem like the most clever way of doing it cause I keep adding depth to the function.
Is there a better way? I tried a recursive function I found here on stack, but couldn't get it to work.
// Look for everyting at level ONE
for (var property in textfield) {
  // Is there a field?
  if (textfield.hasOwnProperty(property)) {
    // Is level ONE an OBJECT
    if (typeof textfield[property] == "object") {
      // Look for everyting at level TWO
      for (var subProperty in textfield[property]) {
        // Is there a field?
        if (textfield[property].hasOwnProperty(subProperty)) {
          // Is level TWO an OBJECT
          if (typeof textfield[property][subProperty] == "object") {
            // Look for everyting at level THREE
            for (var subSubProperty in textfield[property][subProperty]) {
              // Is there a field?
              if (textfield[property][subProperty].hasOwnProperty(subSubProperty)) {
                // Is level THREE an OBJECT
                if (typeof textfield[property][subProperty][subSubProperty] == "object") {
                  // Look for everyting at level FOUR
                  for (var subSubSubProperty in textfield[property][subProperty][subSubProperty]) {
                    // Translate everything at level FOUR
                    console.log("-----------------------LEVEL 4")
                    console.log("LOOP: " + textfield[property][subProperty][subSubProperty][subSubSubProperty]);
                    console.log("TYPE: " + typeof textfield[property][subProperty][subSubProperty][subSubSubProperty]);
                    textfield[property][subProperty][subSubProperty][subSubSubProperty] = replaceFields(textfield[property][subProperty][subSubProperty][subSubSubProperty]);
                  }
                } else {
                  // Translate everything at level THREE
                  console.log("-----------------------LEVEL 3")
                  console.log("LOOP: " + textfield[property][subProperty][subSubProperty]);
                  console.log("TYPE: " + typeof textfield[property][subProperty][subSubProperty]);
                  textfield[property][subProperty][subSubProperty] = replaceFields(textfield[property][subProperty][subSubProperty]);
                }
              }
            }
          } else {
            // Translate everything at level TWO
            console.log("-----------------------LEVEL 2")
            console.log("LOOP: " + textfield[property][subProperty]);
            console.log("TYPE: " + typeof textfield[property][subProperty]);
            textfield[property][subProperty] = replaceFields(textfield[property][subProperty]);
          }
        }
      }
    } else {
      // Translate everything at level ONE
      console.log("-----------------------LEVEL 1")
      console.log("LOOP: " + textfield[property]);
      console.log("TYPE: " + typeof textfield[property]);
      textfield[property] = replaceFields(textfield[property]);
    }
  }
}
Thanks to Jinweizhu's answer below, this is the final version and it works perfectly!!
function translateAll(textfield) {
    for (var property in textfield) {
        if (!textfield.hasOwnProperty(property)) {
            return false;
        } else if (typeof textfield[property] !== "object") {
            textfield[property] = replaceFields(textfield[property]);
        } else {
            translateAll(textfield[property]);
        }
    }
}
 
     
    