Function: array_flip version 14

Function:
Description: Return array with key <-> value flipped.
Version:
Status: latest
Date: 2011-12-20 04:33
Summary: Import from phpjs.
Programmer: jj5
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function array_flip (trans) {
    // http://jsphp.co/jsphp/fn/view/array_flip
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      improved by: Pier Paolo Ramon (http://www.mastersoup.com/)
    // *     example 1: array_flip( {a: 1, b: 1, c: 2} );
    // *     returns 1: {1: 'b', 2: 'c'}
    var key, tmp_ar = {};

    for (key in trans) {
        if (!trans.hasOwnProperty(key)) {continue;}
        tmp_ar[trans[key]] = key;
    }

    return tmp_ar;
}
[top]

Comments

There are no comments yet, be the first!

Please Login or Register to post comments.