关于javascript:算法每日一题

9次阅读

共计 326 个字符,预计需要花费 1 分钟才能阅读完成。

1、json 下划线 key 转驼峰命名

 解题外围:1、正则表达式,/_(\w)/g
2、replace 办法
实现:function convert(jsonObj){let jsonStr = JSON.stringify(jsonObj);
    // 外围正则表白,字符串的 replace 办法
    let formatStr = jsonStr.replace(/_(\w)/g, (all, letter,index,str)=>{return letter.toUpperCase();
    });
    return JSON.parse(formatStr)
  }
  console(convert([{'de_fff_dd':{'n_ff_dd':'222'}},{'ab_cd_ef':'1111'}]))
  

正文完
 0