Giter Site home page Giter Site logo

object-rename-keys's Introduction

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' }
]

object-rename-keys's People

Contributors

nluo avatar jbreuer95 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.