Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Last active August 29, 2015 14:07
Show Gist options
  • Save Dammmien/ee65f29276cc99ef5476 to your computer and use it in GitHub Desktop.
Save Dammmien/ee65f29276cc99ef5476 to your computer and use it in GitHub Desktop.
Super Array
var SA = function( a ) {
a.has = function( v ) {
var i = a.length;
for ( i; i--; )
if ( a[ i ] === v )
return true;
return false;
};
a.each = function( f ) {
var i = 0,
n = a.length;
for ( i; i < n; i++ )
f( a[ i ], i );
return a;
};
a.remove = function( v ) {
var i = a.length;
for ( i; i--; )
if ( a[ i ] === v )
a.splice( i, 1 );
return a;
};
a.replace = function( b, c ) {
var i = a.length;
for ( i; i--; )
if ( a[ i ] === b )
a[ i ] = c;
return a;
};
a.count = function( v ) {
var c = 0,
i = a.length;
for ( i; i--; )
if ( a[ i ] === v )
c++;
return c;
};
a.removeDuplicate = function() {
var i = a.length;
for ( i; i--; )
if ( a.count( a[ i ] ) > 1 )
a.splice( i, 1 );
return a;
};
return a;
};
var foo = SA( [ 1, 2, 3 ] );
foo.push( 'four' );
foo.each( function( a ) {
console.log( a );
} )
foo.remove( 1 );
foo.replace( 2, 'two' );
foo.push( 'two' );
foo.removeDuplicate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment