Replacing Object keys
by 420
JavaScript
// Replacing Object keys
const obj = {name: 'sanjog', age: 25}
const newKey = {name: 'NAME', age: 'AGE'}
// first way
function format(obj, newKey){
const res = Object.keys(obj).reduce((acc, key) => {
return {...acc, [newKey[key]]: obj[key]}
}, {});
console.log(res)
}
format(obj, newKey)
//second way
function format(oldKey, newKey){
const res = Object.keys(oldKey).map(key => {
return {[newKey[key]]: oldKey[key]}
})
const result = Object.assign({}, ...res)
console.log(result)
}
format(oldKey, newKey);