Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Permute //JavaScript Function

DZone's Guide to

Permute //JavaScript Function

·
Free Resource

It permutes elements in an array, the "m" parameter is a boolean which determines if the function should return an array with an "index map" or the real value.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/permute [v1.0]

permute = function( v, m ){
	for( var j, l = v.length, i = ( 1 << l ) - 1, r = new Array( i ); i; )
		for( r[--i] = [], j = l; j; i + 1 & 1 << --j && ( r[i].push( m ? j : v[j] ) ) );
	return r;
};




Example


document.write( permute( ["A", "B", "C" ], 1 ).join( "
" ), "
" ); document.write( permute( ["A", "B", "C" ], 0 ).join( "
" ) );
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}