[FIXED] How to capitalize all keys in an array of objects?

Issue

I need to capitalize each first letter of every key in an array of objects. I tried with my code below but it does not seem to work.

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,
            }

        ]
    }
}]

Solution

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;
}

Answered By – IT goldman

Answer Checked By – Cary Denson (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published