zoukankan      html  css  js  c++  java
  • 记一道面试题

    const testData = {
    a_bbb: 123,
    a_g: [1, 2, 3, 4],
    a_d: {
    s: 2,
    s_d: 3
    },
    a_f: [1, 2, 3, {
    a_g: 'a_g_123'
    }],
    a_d_s: 1
    }

    /**
    * 将一个json数据的所有key从下划线改为驼峰
    *
    * @param {object | array} value 待处理对象或数组
    * @returns {object | array} 处理后的对象或数组
    */
    function mapKeysToCamelCase(data) {
    if (typeof data !== 'object') {
    return data;
    }

    const res = {};
    for(let key in data) {
    const newKey = key.split('_').reduce((res, next) => {
    return res + next[0].toUpperCase() + next.slice(1);
    })
    const value = data[key];

    if (Array.isArray(value)) {
    res[newKey] = value.map(mapKeysToCamelCase);
    } else if (typeof value === 'object') {
    res[newKey] = mapKeysToCamelCase(value);
    } else {
    res[newKey] = value;
    }
    }

    return res;
  • 相关阅读:
    Python标准库--abc模块
    Python标准库--argparse模块
    爬虫基础
    jQuery基础
    前端基础之javascript
    web前端之HTML
    MySQL常用语句
    多线程和多进程
    socket网络编程
    异常处理
  • 原文地址:https://www.cnblogs.com/yjmBlogs/p/10124672.html
Copyright © 2011-2022 走看看