Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript equivalent of PHP function: array_flip

Is there any shortcut to accomplishing the equivalent of PHP's array_flip function in JavaScript or does it have to be done via brute force looping?

It has to be used for dozens of arrays so even small speedups will probably add up.

like image 276
ck_ Avatar asked Jul 21 '09 13:07

ck_


1 Answers

Don't think there's one built in. Example implementation here, though :).

function array_flip( trans ) {     var key, tmp_ar = {};      for ( key in trans )     {         if ( trans.hasOwnProperty( key ) )         {             tmp_ar[trans[key]] = key;         }     }      return tmp_ar; } 
like image 189
Daniel F. Thornton Avatar answered Oct 04 '22 16:10

Daniel F. Thornton