将对象中的所有属性名改为驼峰式, 有点类似简化版的深拷贝 console.log(objectKeyToCamel(person)); function toCamel(str) { return str.replace(/\_(\w)/g, function (match, p1) { return p1.toUpperCase(); }); } function objectKeyToCamel(object) { const ObjectTag = '[object Object]&(14698)
#39;; const ArrayTag = '[object Array]'; const typeTag = getTypeTag(object); let newObject = void 0; let isArray = false; if (typeTag === ObjectTag) { // 不破坏原型 newObject = initCloneObject(object); } else if (typeTag === ArrayTag) { isArray = true; newObject = []; } else { return object; } if (isArray) { object.forEach((value, index) => { newObject[index] = objectKeyToCamel(value); }) } else { let keys = Object.keys(object); for (let key of keys) { let newKey = toCamel(key); newObject[newKey] = objectKeyToCamel(object[key]); } } return newObject; }