Issue
I have a JSON object. I want to transform array to produce following as PascalCase:
contacts: [{ givenName: "Matt", familyName: "Berry" }]
Contacts: [{ GivenName: "Matt", FamilyName: "Berry" }]
The following answer did not work
Does Lodash have a Pascal case function? It did not locate it in runtime.
_.mapKeys(obj, (v, k) => _.camelCase(k))
Currently using the Typescript Angular 10 library .
Convert returned JSON Object Properties to (lower first) camelCase
Looking to loop through all members of Array of object,
Solution
PascalCase seems to be almost the same as camelCase, only difference being the first letter is capitalized in PascalCase. So you should be able to use the camelCase function from lodash and then just capitalize the first letter of the camelCase word with another function, e.g. something like this:
const capitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1)
}
So the whole solution is probably something like this:
_.mapKeys(obj, (v, k) => capitalize(_.camelCase(k)))
Answered By - Pete
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.