Underscore: Ajouter une fonction pour renommer les clés dans les objets

Créé le 9 juil. 2013  ·  1Commentaire  ·  Source: jashkenas/underscore

Ajouter une fonction pour renommer les clés dans un objet

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

ou un tableau d'objets

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

Merci,
Nisanth

enhancement wontfix

>Tous les commentaires

Merci, mais non merci -- cas bien trop spécial pour le noyau Underscore. N'hésitez pas à le mélanger à vos propres fins.

Cette page vous a été utile?
0 / 5 - 0 notes