object-rename-keys

1.2.1 • Public • Published

Build Status

What

Returns a copy of the object with keys changed to values defined by a changeMap object. If an array of objects is passed in, then a new array with a copy of the changed object will be returned.

Quick Reference

Install

yarn add object-rename-keys
//or
npm i --save object-rename-keys

API

Require and use it:

var objectRenameKeys = require('object-rename-keys');
objectRenameKeys(object, changesMap);
  • object: the original object
  • changesMap: the changesMap is the object you defined what keys you would like to change, and the values changed to.
changesMap = {
  originalKey: newValue,
  ...
}

and an example object:

var address = {
  street: 'Hollywood Street',
  suburb: 'Calamvale',
  country: 'AUS'
};

Let's say in your database the names of some fields are different, e.g. addressStreet instead of street, Suburb instead of suburb etc. So you want to change the keys of the object to:

var changes = {
    street: 'myStreet',
    suburb: 'Suburb'
};

Apply it:

var result = objectRenameKeys(address, changes);
console.log(result);

will have result output:

{
  country: "AUS",
  myStreet: "Hollywood Street",
  Suburb: "Calamvale"
}

Nested

It will also go through the object recurisively if you specific the keys in the changesMap in a nested way

var addressObject = {
  address: {
    street: 'Hollywood Street',
    suburb: 'Calamvale',
    country: 'AUS'
  }
}
var changes = {
  address: {
    street: 'myStreet'
  },
  suburb: 'Suburb'
}
 
var result = objectRenameKeys(addressObject, changes);
console.log(result);

will output:

{
  address: {
    suburb: 'Calamvale',
    country: 'AUS',
    myStreet: 'Hollywood Street'
  }
}

Array of objects

If an array of objects is passed in, then a new array with a copy of the changed object will be returned.

var addressObjs = [
  addressObject,
  {
    suburb: 'Mosman'
  }
]
 
var changes = {
  address: {
    street: 'myStreet'
  },
  suburb: 'Suburb'
}
 
var result = objectRenameKeys(addressObjs, changes);
console.log(result);

will output:

[
  {
    address:
      {
        suburb: 'Calamvale',
        country: 'AUS',
        myStreet: 'Hollywood Street'
      }
  },
  { Suburb: 'Mosman' }
]
 

Readme

Keywords

none

Package Sidebar

Install

npm i object-rename-keys

Weekly Downloads

465

Version

1.2.1

License

MIT

Unpacked Size

15 kB

Total Files

7

Last publish

Collaborators

  • nick.luo