将对象中的所有属性名改为驼峰式, 有点类似简化版的深拷贝 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]'; 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; }
点赞 1

相关推荐

10-24 16:47
华南理工大学
点赞 评论 收藏
分享
牛客网
牛客企业服务