亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將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 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號