ck_ ck_ - 1 year ago 238
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.

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download