How to capitalize objects key in a nested arrays of objects ? . So I need to capitalize each first letter of every keys in an array of objects. I tried with my code below but it does not seem no work , anyone has an idea.? Thanks.
#current code
capitalizeObjectKeys(obj) {
return _.transform(obj, function (result, val, key:any) {
result[key.toUpperCase()] = val;
});
}
#sample object
[{
"id": 3,
"accountId": 6,
"title": "ABCDF Copy",
"versionDto": {
"id": 3,
"entitlementsTemplateId": 3,
"status": "Draft",
"entitlementElementsTemplateDto": [
{
"id": 5,
"isHeaderCategory": false,
"order": 1,
},
{
"id": 6,
"isHeaderCategory": false,
"order": 2,
}
]
}
}]
#expected output
[{
"Id": 3,
"AccountId": 6,
"Title": "ABCDF Copy",
"VersionDto": {
"Id": 3,
"EntitlementsTemplateId": 3,
"Status": "Draft",
"EntitlementElementsTemplateDto": [
{
"Id": 5,
"IsHeaderCategory": false,
"Order": 1,
},
{
"Id": 2,
"IsHeaderCategory": false,
"Order": 2,
}
]
}
}]
CodePudding user response:
Let's iterate recursively the entire object using Object.keys(obj)
const capitalize = (obj) => {
if (!obj) {
return;
}
Object.keys(obj).forEach(key => {
var value = obj[key]
if (typeof value === "object" && value !== null) {
capitalize(value)
}
var Key = key[0].toUpperCase() key.substr(1);
delete obj[key];
obj[Key] = value;
})
}
var obj = [{
"id": 3,
"accountId": 6,
"title": "ABCDF Copy",
"versionDto": {
"id": 3,
"entitlementsTemplateId": 3,
"status": "Draft",
"entitlementElementsTemplateDto": [{
"id": 5,
"isHeaderCategory": false,
"order": 1,
},
{
"id": 6,
"isHeaderCategory": false,
"order": 2,
}
]
}
}]
var object2 = {
"id": 2,
"accountId": 6,
"title": "TEMPLATE 2",
"entitlementsTemplateVersionDto": {
"id": 2,
"entitlementsTemplateId": 2,
"status": "Draft",
"entitlementElementsTemplateDto": [{
"id": 2,
"entitlementsTemplateVersionId": 2,
"entitlementName": "TEMPLATE C",
"isHeaderCategory": true,
"order": 1,
"isRequired": false,
"entitlementSubCategoryElementsTemplateDto": [{
"id": 1,
"entitlementElementsTemplateId": 2,
"entitlementName": "",
"order": 0,
"isRequired": false
},
{
"id": 2,
"entitlementElementsTemplateId": 2,
"entitlementName": "",
"order": 0,
"isRequired": false
}
]
},
{
"id": 3,
"entitlementsTemplateVersionId": 2,
"entitlementName": "TEMPLATE B",
"isHeaderCategory": false,
"order": 1,
"isRequired": false,
"entitlementSubCategoryElementsTemplateDto": []
},
{
"id": 4,
"entitlementsTemplateVersionId": 2,
"entitlementName": "TEMPLATE A",
"isHeaderCategory": false,
"order": 1,
"isRequired": true,
"entitlementSubCategoryElementsTemplateDto": []
}
]
}
}
capitalize(obj);
console.log(obj)
capitalize(object2);
console.log(object2)
.as-console-wrapper {
max-height: 100% !important;
}
CodePudding user response:
It looks like the issue is with your capitalization method.
The following alternative should work:
const capitalizeKey = (key) => key.charAt(0).toUpperCase() key.slice(1);
Here's a very basic live demo:
const capitalizeObjectKeys = (obj) => {
if (!obj) return;
if (Array.isArray(obj)) {
return obj.map((each) => capitalizeObjectKeys(each))
}
const results = {};
Object.keys(obj).forEach((key) => {
const newKey = key.charAt(0).toUpperCase() key.slice(1);
const objVal = (typeof obj[key] === 'object') ? capitalizeObjectKeys(obj[key]) : obj[key];
results[newKey] = objVal;
});
return results;
};
const sample = [{"id":3,"accountId":6,"title":"ABCDF Copy","versionDto":{"id":3,"entitlementsTemplateId":3,"status":"Draft","entitlementElementsTemplateDto":[{"id":5,"isHeaderCategory":false,"order":1},{"id":6,"isHeaderCategory":false,"order":2}]}}];
const results = capitalizeObjectKeys(sample)
console.log(results);