rlueder
8/8/2017 - 9:51 PM

lodash helpers - rename (renames object keys)

lodash helpers - rename (renames object keys)

/*
* var person = { firstName: 'bill', lastName: 'johnson' }
* 
* person = _.rename(person, 'firstName', 'first')
* person = _.rename(person, 'lastName', 'last')
*
* console.log(person) // { first: 'bill', last: 'johnson' }
*/

_.rename = (obj, key, newKey) => {
  if(_.includes(_.keys(obj), key)) {
    obj[newKey] = _.clone(obj[key], true)
    delete obj[key]
  }
  return obj
}