看别人的面经发现一道题,一时手痒就写了下,不知道对错。题源:https://www.nowcoder.com/discuss/470638184494276608const underscoreToCamelCase = (data) => {  if (Array.isArray(data)) {    return data.map(underscoreToCamelCase);  } else if (typeof data === "object" && data != null) {    const res = {};    for (const key in data) {      let newKey = "";      if (key.indexOf("_") != -1) {        let upper = false;        for (let i = 0; i < key.length; i++) {          if (key.charAt(i) === "_") {            upper = true;            continue;          }          newKey += upper ? key.charAt(i).toUpperCase() : key.charAt(i);          upper = false;        }      }      res[newKey ? newKey : key] = underscoreToCamelCase(data[key]);    }    return res;  } else {    return data;  }};// 测试const data = {  first_name: "John",  last_name: "Doe",  age: 30,  address: {    city_name: "New York",    country_code: "US",  },  hobbies: ["reading_books", "playing_games"],};console.table(underscoreToCamelCase(data));
点赞 1
评论 0
全部评论

相关推荐

東大沒有派對:这是好事啊(峰哥脸
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务