Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap keys and values in dictionary with underscore

Lets say we have a dictionary with unique values:

{ a: 1, b: 2 }

and we would like to swap keys and values like:

{ 1: 'a', 2: 'b' }

how to do it with underscore?

like image 822
user2846569 Avatar asked Feb 10 '15 08:02

user2846569


1 Answers

In underscore.js there is method _.invert

console.log(_.invert({ a: 1, b: 2 }))
<script src="//jashkenas.github.io/underscore/underscore-min.js"></script>
like image 182
Oleksandr T. Avatar answered Oct 20 '22 08:10

Oleksandr T.