JSON.stringify,Tree UI 함수 작성하기,.

임성은·2022년 12월 16일

JSON.stringify

 */
function stringifyJSON(obj) {
  //객체는 문자열 "9"로 변환되어야 합니다 숫자를 문자로 변환 Strting//객체는 문자열 "true","false"로 변환되어야 합니다
  if (typeof obj === "number" ||typeof obj === "boolean" ) {
    return obj.toString();
  }
  //객체는 문자열 "null"로 변환되어야 합니다
  if (obj === null) {
    return "null";
  }
  if (typeof obj === "string" ) {
    return `"${obj}"`
  }
  //배열일 경우 -> ..[]요소가 그대로 들어가야한다.
  if(Array.isArray(obj)){
    let arr1 = []
    for(let el of obj){
      arr1.push(stringifyJSON(el))
      
    }
    return `[${arr1}]`
  }

  //객체일 경우
  if(typeof obj === "object"){
    let obj2 = "";
    
    if(Object.keys(obj).length === 0){
      return `{}`
    }
    for(let key in obj){
      if(obj[key] === undefined || typeof obj[key] === "function" ){
        return `{}`
      }
      obj2 = obj2 +`${stringifyJSON(key)}:${stringifyJSON(obj[key])},` 
    }
    let newobj = obj2.slice(0, -1)
    console.log(newobj)
    return `{${newobj}}`
  }



}

// 다음 코드는 결과 제출을 위한 코드입니다. 신경 쓰지 않아도 좋습니다.
if (typeof window === "undefined") {
  module.exports = stringifyJSON;
}

Tree Ui

const root = document.getElementById('root');    


function createTreeView(menu, currentNode) {
 for(let i =0; i<menu.length; i++){
 if(menu[i].children){
  const input = document.createElement('input')
  const li = document.createElement("li");
  const span = document.createElement("span");
  const ul = document.createElement("ul");
  
  input.type = 'checkbox'
  span.textContent = menu[i].name;
  li.append(input,span,ul)
  currentNode.append(li)
  createTreeView(menu[i].children, ul)
 }else{
  const li = document.createElement('li')
  li.textContent = menu[i].name
  currentNode.append(li)
 }
 }


  /// recursive case 재귀 호출을 해줍니다.


}

createTreeView(menu, root);

코드...

profile
개발자의 길에 당차게 들어서다!!

0개의 댓글