Skip to content

Instantly share code, notes, and snippets.

@achudars
Last active December 17, 2015 00:19
Show Gist options
  • Save achudars/5520409 to your computer and use it in GitHub Desktop.
Save achudars/5520409 to your computer and use it in GitHub Desktop.
JavaScript array_flip function - A JavaScript equivalent of PHP’s array_flip
function array_flip (trans) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Pier Paolo Ramon (http://www.mastersoup.com/)
// + improved by: Brett Zamir (http://brett-zamir.me)
// * example 1: array_flip( {a: 1, b: 1, c: 2} );
// * returns 1: {1: 'b', 2: 'c'}
// * example 2: ini_set('phpjs.return_phpjs_arrays', 'on');
// * example 2: array_flip(array({a: 0}, {b: 1}, {c: 2}))[1];
// * returns 2: 'b'
var key, tmp_ar = {};
if (trans && typeof trans=== 'object' && trans.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
return trans.flip();
}
for (key in trans) {
if (!trans.hasOwnProperty(key)) {continue;}
tmp_ar[trans[key]] = key;
}
return tmp_ar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment