ck_ ck_ - 1 month ago 11
Javascript Question

array_flip in javascript?

Is there any shortcut to accomplishing the equivalent of PHP's array_flip 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.


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;