I want to replace the key space in a nested object. I have an object as follows:
var data =
{ 'General Information':
{ 'Referral No': '123123',
Marketer: '',
Casemanager: 'Alexis Clark',
'CM Username': '',
VOC: '',
'Foreign Voluntary': '',
},
'Account Name': 'CTS Health',
}
What I've done:
for (var k in data) {
if (k.replace(/\s/g, '') !== k) {
data[k.replace(/\s/g, '')] = data[k];
if (data[k] !== null && typeof data[k] === 'object') {
for (var x in data[k]) {
if (x.replace(/\s/g, '') !== x) {
data[k][x.replace(/\s/g, '')] = data[k][x];
delete data[k][x];
}
}
}
delete data[k];
}
}
I get this:
{ GeneralInformation:
{ 'Referral No': '123123',
Marketer: '',
Casemanager: 'Alexis Clark',
'CM Username': '',
VOC: '',
'Foreign Voluntary': '',
},
AccountName: 'CTS Health',
}
I want:
{ GeneralInformation:
{ ReferralNo: '123123',
Marketer: '',
Casemanager: 'Alexis Clark',
CMUsername: '',
VOC: '',
ForeignVoluntary: '',
},
AccountName: 'CTS Health',
}
What am I doing wrong here?