为了账号安全,请及时绑定邮箱和手机立即绑定

将json api文档缩减为驼峰式

将json api文档缩减为驼峰式

一只名叫tom的猫 2022-06-05 16:33:52
我想将 json api 标准文档简化为骆驼版本。我能够制作一个camelCase对象和单个原始类型键。我希望所需的输出是每个键camelCase。所以在上面的例子中first-name会变成firstName,snake-case会变成snakeCase。我面临的问题是如何使用我现在使用的相同递归调用来处理对象数组。import _ from "lodash";const data = {  id: 1,  type: "user",  links: { self: "/movies/1" },  meta: { "is-saved": false },  "first-name": "Foo",  "last-name": "Bar",  locations: ["SF"],  actors: [    { id: 1, type: "actor", name: "John", age: 80 },    { id: 2, type: "actor", name: "Jenn", age: 40 }  ],  awards: [    {      id: 4,      type: "Oscar",      links: ["asd"],      meta: ["bar"],      category: "Best director",      'snake_case': 'key should be snakeCase'    }  ],  name: { id: 1, type: "name", title: "Stargate" }};const needsCamelCase = str => {  return str.indexOf("-") > -1 || str.indexOf("_") > -1;};const strToCamelCase = function(str) {  return str.replace(/^([A-Z])|[\s-_](\w)/g, function(match, p1, p2, offset) {    if (p2) return p2.toUpperCase();    return p1.toLowerCase();  });};const toCamelCase = obj => {  Object.keys(obj).forEach(key => {    if (_.isPlainObject(obj[key])) {      return toCamelCase(obj[key]);    }    if (_.isArray(obj[key])) {      // console.log(obj[key]);      obj[key].forEach(element => {        console.log(element);      });      //obj[key].foreach(element_ =>  toCamelCase);    }    if (needsCamelCase(key)) {      obj[strToCamelCase(key)] = obj[key];      delete obj[key];    }  });  return obj;};// toCamelCase(data);console.log(toCamelCase(data));这是一个代码框: https ://codesandbox.io/s/javascript-l842u
查看完整描述

1 回答

?
茅侃侃

TA贡献1842条经验 获得超21个赞

逻辑很简单:如果它是一个对象,只需调用toCamelCase,如果它是一个数组,则对其进行迭代以创建一个新数组。如果它是一个对象数组,用 转换它toCamelCase,如果它是一个其他东西的数组,保持原样。


解决方案可能如下所示:


const _ = require('lodash');


const data = {

  id: 1,

  type: "user",

  links: { self: "/movies/1" },

  meta: { "is-saved": false },

  "first-name": "Foo",

  "last-name": "Bar",

  locations: ["SF"],

  actors: [

    { id: 1, type: "actor", name: "John", age: 80 },

    { id: 2, type: "actor", name: "Jenn", age: 40 }

  ],

  awards: [

    {

      id: 4,

      type: "Oscar",

      links: ["asd"],

      meta: ["bar"],

      category: "Best director",

      'snake_case': 'key should be snakeCase'

    }

  ],

  name: { id: 1, type: "name", title: "Stargate" }

};


const needsCamelCase = str => {

  return str.indexOf("-") > -1 || str.indexOf("_") > -1;

};


const strToCamelCase = function(str) {

  return str.replace(/^([A-Z])|[\s-_](\w)/g, function(match, p1, p2, offset) {

    if (p2) return p2.toUpperCase();

    return p1.toLowerCase();

  });

};


const toCamelCase = obj => {

  Object.keys(obj).forEach(key => {

    const camelCasedKey = needsCamelCase(key) ? strToCamelCase(key) : key;

    const value = obj[key];

    delete obj[key];

    obj[camelCasedKey] = value;


    if (_.isPlainObject(value)) {

      obj[camelCasedKey] = toCamelCase(value);

    }


    if (_.isArray(value)) {

      obj[camelCasedKey] = value.map(item => {

        if (_.isPlainObject(item)) {

          return toCamelCase(item);

        } else {

          return item;

        }

      });

    }

  });


  return obj;

};


// toCamelCase(data);

console.log(toCamelCase(data));


查看完整回答
反对 回复 2022-06-05
  • 1 回答
  • 0 关注
  • 158 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信