My JavaScript object looks like this:
result = [
{"Account_ID__r.Name":"Yahoo Inc Taiwan","Contact_ID__r.Email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="42283427302c2d2e2602362a27313623306c212d2f">[email protected]</a>"},
{"Account_ID__r.Name":"WXIA-TV","Contact_ID__r.Email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6ccd1c9c9cac4d4cfc1ced2e69797c388c5c9cb">[email protected]</a>"}
]
I'm looking to modify the keys in the object by removing periods, for example changing Account_ID__r.Name
to Account_ID__rName
.
The updated array should look like this:
result = [
{"Account_ID__rName":"Yahoo Inc Taiwan","Contact_ID__rEmail":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3c9d5c6d1cdcccfc7e3d7cbc6d0d7c2d18dc0ccce">[email protected]</a>"},
{"Account_ID__rName":"WXIA-TV","Contact_ID__rEmail":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="472d3028282b25352e202f33077676226924282a">[email protected]</a>"}
]
I attempted to create a new array using a
for (var key of Object.keys(result)) { }
loop, but it was unsuccessful.
This is the code I tried:
result.forEach(item => {
for (var key of Object.keys(item)) {
var keyWithoutPeriod = key.replace(/\./g,'');
this.datatableData.push({
[keyWithoutPeriod]: item[key]
});
}
});
The resulting datatable looks like this:
datatable = [
{"Account_ID__rName":"Yahoo Inc Taiwan"},{"Contact_ID__rEmail":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="117b6774637f7e7d7551657974626570633f727e7c">[email protected]</a>"},
{"Account_ID__rName":"WXIA-TV"},{"Contact_ID__rEmail":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a607d6565666878636d627e4a3b3b6f24696567">[email protected]</a>"}
]
This structure is not what I require. Any assistance would be greatly appreciated. Thank you! 😃