Underscore: Add function to rename keys in objects

Created on 9 Jul 2013  路  1Comment  路  Source: jashkenas/underscore

Add function to rename keys in an object

> var _ = require('underscore');
> var note = {
... '_id': 1,
... 'title': 'Ruby is awesome..',
... 'text': '..and so is CoffeeScript'
... }
> console.log(_.rename(note, [['_id', 'id']]));
{ id: 1,
  title: 'Ruby is awesome..',
  text: '..and so is CoffeeScript' }

or an array of objects

> var notes = [note, note];
> console.log(_.rename(notes, [['_id', 'id']]));
[ { id: 1,
    title: 'Ruby is awesome..',
    text: '..and so is CoffeeScript' },
  { id: 1,
    title: 'Ruby is awesome..',
    text: '..and so is CoffeeScript' } ]

Thanks,
Nisanth

enhancement wontfix

>All comments

Thanks, but no thanks -- way too special case for core Underscore. Feel free to mix it in for your own purposes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

githublyp picture githublyp  路  3Comments

acl0056 picture acl0056  路  5Comments

jezen picture jezen  路  8Comments

jdalton picture jdalton  路  6Comments

clouddueling picture clouddueling  路  3Comments