How to rename object keys inside array
I strongly suggest the usage of a key replacement map over a simple list of new keys, for the latter is strongly depended on a customer object's key order.
If a customer object satisfies a 1:1 key mapping, go for an approach similar to this one, that maps a list of customer objects by creating a new customer object with each iteration step via reducing a list of key tuples with each tuple holding the old and the new key ...
function createNewCustomerFromOldOneViaBoundConfig(customer) {
return Object.entries(this).reduce((newCustomer, [key, newKey]) => {
newCustomer[newKey] = customer[key];
return newCustomer;
}, {});
};
const customerKeyReplacementMap = {
customer_name: 'firstname',
customer_age: 'age',
customer_weapon: 'weapon',
customer_email: 'email',
customer_city: 'city'
};
const customers = [{
customer_name: 'Negan',
customer_age: 45,
customer_weapon: 'Bat',
customer_email: '[email protected]',
customer_city: 'Washington'
}, {
customer_name: 'Daryl',
customer_age: 41,
customer_weapon: 'Crossbow',
customer_email: '[email protected]',
customer_city: 'Atlanta'
}, {
customer_name: 'Rick',
customer_age: 45,
customer_weapon: 'Magnum 357',
customer_email: '[email protected]',
customer_city: 'King County'
}].map(createNewCustomerFromOldOneViaBoundConfig, customerKeyReplacementMap);
console.log('customers : ', customers);
.as-console-wrapper { min-height: 100%!important; top: 0; }
As soon as at least one customer object violates a strict 1:1 mapping of its keys, one has to change the approach to creating and mutating a new customer object from its outdated counterpart.
This case also proves that any approach based on just a list of replacement keys is really limited to just a single kind of a customer object's key order (and structure) ...
function createNewCustomerFromOldOneAndMutateKeysViaBoundConfig(oldCustomer) {
return Object.entries(this).reduce((customer, [oldKey, key]) => {
customer[key] = customer[oldKey];
delete customer[oldKey];
return customer;
}, Object.assign({}, oldCustomer));
};
const customerKeyReplacementMap = {
customer_name: 'firstname',
customer_age: 'age',
customer_weapon: 'weapon',
customer_email: 'email',
customer_city: 'city'
};
const customers = [{
additional_key_1: 'FOO',
customer_name: 'Negan',
customer_age: 45,
additional_key_2: 'BAR',
customer_weapon: 'Bat',
customer_email: '[email protected]',
customer_city: 'Washington'
}, {
additional_key_1: 'BAZ',
customer_name: 'Daryl',
customer_age: 41,
additional_key_2: 'BIZ',
customer_weapon: 'Crossbow',
customer_email: '[email protected]',
customer_city: 'Atlanta'
}, {
additional_key_1: 'FOOBAR',
customer_name: 'Rick',
customer_age: 45,
additional_key_2: 'BAZBIZ',
customer_weapon: 'Magnum 357',
customer_email: '[email protected]',
customer_city: 'King County'
}].map(
createNewCustomerFromOldOneAndMutateKeysViaBoundConfig,
customerKeyReplacementMap
);
console.log('customers : ', customers);
.as-console-wrapper { min-height: 100%!important; top: 0; }
You can iterate over the objects, and then change the keys
of each property by the ones in newKeys
:
const customers = [
{
customer_name: 'Negan',
customer_age: 45,
customer_weapon: 'Bat',
customer_email: '[email protected]',
customer_city: 'Washington'
},
{
customer_name: 'Daryl',
customer_age: 41,
customer_weapon: 'Crossbow',
customer_email: '[email protected]',
customer_city: 'Atlanta'
},
{
customer_name: 'Rick',
customer_age: 45,
customer_weapon: 'Magnum 357',
customer_email: '[email protected]',
customer_city: 'King County'
},
]
const newKeys = [
'firstname',
'age',
'weapon',
'email',
'city'
]
for (let i = 0; i < customers.length; i++) {
let customer = customers[i];
let j = 0;
for(let p in customer){
customer[newKeys[j++]] = customer[p];
delete customer[p];
}
}
console.log(customers);
You can use Object.values()
to retrieve values and then array.reduce()
to compose a new object:
const customers = [
{
customer_name: 'Negan',
customer_age: 45,
customer_weapon: 'Bat',
customer_email: '[email protected]',
customer_city: 'Washington'
},
{
customer_name: 'Daryl',
customer_age: 41,
customer_weapon: 'Crossbow',
customer_email: '[email protected]',
customer_city: 'Atlanta'
},
{
customer_name: 'Rick',
customer_age: 45,
customer_weapon: 'Magnum 357',
customer_email: '[email protected]',
customer_city: 'King County'
},
];
const newKeys = [
'firstname',
'age',
'weapon',
'email',
'city'
];
let result = customers.map(obj =>
Object.values(obj).reduce((acc, cur, i) => {
acc[newKeys[i]] = cur;
return acc; }, {}));
console.log(result);