Solución

@josemcarrizosa98_f7e20d3d
·22/3/2026TypeScript
solution.tsTypeScript
function flattenObject(obj: Record<string, unknown>, prefix = ""): Record<string, unknown> {
  const keys = Object.keys(obj);
  if (keys.length == 0) return {};
  const flatObject: Record<string, unknown> = {}
  processLeaveValue(obj, flatObject, []);
  return flatObject;
}


function processLeaveValue(currentValue: unknown, flatObject: Record<string, unknown>, composedKey: string[] = [] ){
  if (typeof currentValue != "object"){
      flatObject[composedKey.join(".")]= currentValue;
      return true;
  }

  const keys = Object.keys(currentValue);
  if(!keys || ! keys.length){
      if (composedKey.length == 0){
        return true;
      }else{
        flatObject[composedKey.join(".")]= currentValue;
        return true
      }
  }

  keys.forEach((currentKey, index) => {
    composedKey.push(currentKey);
    
    console.log(composedKey, currentKey)
    const valueWasAdded = processLeaveValue(currentValue[currentKey], flatObject, composedKey)
    if(valueWasAdded){
      composedKey.pop()
    }
    if(index === (keys.length-1)){
      composedKey.shift();
    }
  })

}
// No modificar: necesario para evaluar el resultado.
export { flattenObject };
0respuestas
Respuestas
0

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.
Markdown