Object.prototype.changeKey = function (keyMap) {
if (!keyMap) {
return this
}
if (keyMap && typeof keyMap !== 'object') {
throw Error('keyMap must be Obejct')
}
return Object.keys(this).reduce((prev, cur) => {
prev[keyMap[cur]] = this[cur]
return prev
}, {})
}
let keyMap = {
org_name: 'orgName',
org_code: 'orgCode'
}
// use
let data = {
org_name: 'mxx',
org_code: '212121'
}
function proxyKey(data) {
return new Proxy(data, {
get(target, key, receiver) {
const newKey = key.replace(/([A-Z])/g, "_$1").toLowerCase()
if (!target[newKey]) {
throw Error('this key is error')
}
return target[newKey]
}
})
}
let newData = proxyKey(data)
console.log(newData.orgCode)
console