Convert returned JSON Object Properties to (lower first) camelCase

后端 未结 18 2276
忘掉有多难
忘掉有多难 2020-12-04 21:05

I have JSON returned from an API like so:

Contacts: [{ GivenName: "Matt", FamilyName: "Berry" }]

To keep this consistent

18条回答
  •  一整个雨季
    2020-12-04 21:25

    Using lodash, you can do it like this:

    export const toCamelCase = obj => {
      return _.reduce(obj, (result, value, key) => {
        const finalValue = _.isPlainObject(value) || _.isArray(value) ? toCamelCase(value) : value;
        return { ...result, [_.camelCase(key)]: finalValue };
      }, {});
    };
    

提交回复
热议问题