Underscore.JS – invert method

Invert method swaps the key and values of object passed.

var result = _.invert({one: 1, two : 2, three: 3});
console.log(result);

// Example 2
result = _.invert({ name: 'Sam', age: 30});
console.log(result);

Output

{ '1': 'one', '2': 'two', '3': 'three' }
{ '30': 'age', Sam: 'name' }

Leave a comment

Your email address will not be published. Required fields are marked *