如何将对象属性数组转换为来自响应的动态驼峰命名法

How to convert an Array of Object properties to camelCase which comes as dynamically from the response

我收到如下回复,

[
   {
      Name: "First Field Value", Second Field Name: "Second Field Value", Third Field Name: "Third Field Value"
   },
   {
      First Object Field: "First Field Value", Second Object Field: "Second Field Value", Third Object Field: "Third Field Value"
   },
]

我想将对象第一个元素(如名称、第二个字段名称、第三个字段名称等)的数组转换为驼峰式。有人可以推荐一下吗?

您应该可以使用 lodash and the _.mapKeys and _.camelCase 函数来完成此操作。

你会 运行 _.mapKeys 每个对象,并将每个键转换为驼峰式大小写。

例如:

let arr = [
   {
      Name: "First Field Value", 'Second Field Name': "Second Field Value", 'Third Field Name': "Third Field Value"
   },
   {
      'First Object Field': "First Field Value", 'Second Object Field': "Second Field Value", 'Third Object Field': "Third Field Value"
   },
{
      'works-for-kebab-case': "First Field Value", 'and_snake_case_too': "Second Field Value", 'AndPascalCase': "Third Field Value"
   },
]


let result = arr.map(el => _.mapKeys(el, (val, key) => _.camelCase(key)));
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>