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

clouddueling picture clouddueling  ·  3Comments

marcalj picture marcalj  ·  5Comments

danilopolani picture danilopolani  ·  5Comments

jdalton picture jdalton  ·  4Comments