LoginSignup
11
8

More than 5 years have passed since last update.

camelcase-keys で snake_case 等を camelCase に変換する

Posted at

バックエンドからのレスポンスはスネークケースの場合が多い。
しかし、フロント側では通常キャメルケースで扱うことを前提としている。
そこで camelcase-keys を使ってレスポンスを変換する。

$ npm install camelcase-keys

以下の例では元データがケバブケースしかないが、もちろんスネークケースもキャメルにできる。

import camelcaseKeys from 'camelcase-keys'

// Convert an object
camelcaseKeys({'foo-bar': true});
//=> {fooBar: true}

// Convert an array of objects
camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);
//=> [{fooBar: true}, {barFoo: false}]

camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true});
//=> {fooBar: true, nested: {unicornRainbow: true}}
11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8