Nodejs Utililty Methods Array Swap

List of utility methods to do Array Swap

Description

The list of methods to do Array Swap are organized into topic(s).

Method

swapByValue( v1, v2 )
Array.prototype.swapByValue = function( v1, v2 ) {
  var i1 = null, i2 = null;
  v1 = checkFn( v1 );
  v2 = checkFn( v2 );
  for( var i = 0; i < this.length; i++ )
    if      ( this[i] === v1 ) i1 = i;
    else if( this[i] === v2 ) i2 = i;
  if( i1 === null ) { throw new Error( 'No element with value \'' + v1 + '\' was found in array.' ); return; }
  if( i2 === null ) { throw new Error( 'No element with value \'' + v2 + '\' was found in array.' ); return; }
...