Array.prototype
Array . prototype. myFlat = function ( depth = 1 ) {
if ( depth < 1 ) return this ;
return this . reduce ( ( flat, toFlatten ) => {
return flat. concat ( Array. isArray ( toFlatten) ? toFlatten. myFlat ( depth - 1 ) : toFlatten) ;
} , [ ] ) ;
} ;
console. log ( [ 1 , [ 2 , [ 3 , [ 4 ] ] , 5 ] ] . myFlat ( 2 ) ) ;
Array . prototype. myForEach = function ( callback, thisArg ) {
for ( let i = 0 ; i < this . length; i++ ) {
if ( this . hasOwnProperty ( i) ) {
callback . call ( thisArg, this [ i] , i, this ) ;
}
}
} ;
[ 1 , 2 , 3 ] . myForEach ( ( num, index ) => console. log ( num, index) ) ;
Array . prototype. myMap = function ( callback, thisArg ) {
let result = [ ] ;
for ( let i = 0 ; i < this . length; i++ ) {
if ( this . hasOwnProperty ( i) ) {
result. push ( callback . call ( thisArg, this [ i] , i, this ) ) ;
}
}
return result;
} ;
console. log ( [ 1 , 2 , 3 ] . myMap ( num => num * 2 ) ) ;
Array . prototype. myFilter = function ( callback, thisArg ) {
let result = [ ] ;
for ( let i = 0 ; i < this . length; i++ ) {
if ( this . hasOwnProperty ( i) && callback . call ( thisArg, this [ i] , i, this ) ) {
result. push ( this [ i] ) ;
}
}
return result;
} ;
console. log ( [ 1 , 2 , 3 ] . myFilter ( num => num > 1 ) ) ;
Array . prototype. myReduce = function ( callback, initialValue ) {
let accumulator = initialValue === undefined ? this [ 0 ] : initialValue;
let startIndex = initialValue === undefined ? 1 : 0 ;
for ( let i = startIndex; i < this . length; i++ ) {
if ( this . hasOwnProperty ( i) ) {
accumulator = callback ( accumulator, this [ i] , i, this ) ;
}
}
return accumulator;
} ;
console. log ( [ 1 , 2 , 3 ] . myReduce ( ( acc, num ) => acc + num, 0 ) ) ;
Array . prototype. myFill = function ( value, start = 0 , end = this . length ) {
start = start < 0 ? Math. max ( this . length + start, 0 ) : Math. min ( start, this . length) ;
end = end < 0 ? Math. max ( this . length + end, 0 ) : Math. min ( end, this . length) ;
for ( let i = start; i < end; i++ ) {
this [ i] = value;
}
return this ;
} ;
console. log ( [ 1 , 2 , 3 ] . myFill ( 0 , 1 , 2 ) ) ;
Array . prototype. myIncludes = function ( value, fromIndex = 0 ) {
fromIndex = fromIndex < 0 ? Math. max ( this . length + fromIndex, 0 ) : fromIndex;
for ( let i = fromIndex; i < this . length; i++ ) {
if ( this [ i] === value) {
return true ;
}
}
return false ;
} ;
console. log ( [ 1 , 2 , 3 ] . myIncludes ( 2 ) ) ;
Array . prototype. myPush = function ( ... args) {
for ( let i = 0 ; i < args. length; i++ ) {
this [ this . length] = args[ i] ;
}
return this . length;
} ;
let arr = [ 1 , 2 , 3 ] ;
arr. myPush ( 4 , 5 ) ;
console. log ( arr) ;
Array . prototype. myUnshift = function ( ... args) {
this . splice ( 0 , 0 , ... args) ;
return this . length;
} ;
let arr = [ 1 , 2 , 3 ] ;
arr. myUnshift ( 0 ) ;
console. log ( arr) ;
Array . prototype. myCopy = function ( ) {
return this . slice ( ) ;
} ;
let arr = [ 1 , 2 , 3 ] ;
let copy = arr. myCopy ( ) ;
copy[ 0 ] = 0 ;
console. log ( arr) ;
console. log ( copy) ;
Array . prototype. getLevel = function ( ) {
const getDepth = ( arr ) => {
return Array. isArray ( arr) ? 1 + Math. max ( 0 , ... arr. map ( getDepth) ) : 0 ;
} ;
return getDepth ( this ) ;
} ;
console. log ( [ 1 , [ 2 , [ 3 , [ 4 ] ] ] ] . getLevel ( ) ) ;
Array . prototype. myIterator = function * ( ) {
for ( let i = 0 ; i < this . length; i++ ) {
yield this [ i] ;
}
} ;
let arr = [ 1 , 2 , 3 ] ;
let iterator = arr. myIterator ( ) ;
console. log ( iterator. next ( ) . value) ;
console. log ( iterator. next ( ) . value) ;
console. log ( iterator. next ( ) . value) ;
Array . prototype. mySort = function ( compareFunction ) {
if ( compareFunction === undefined ) {
compareFunction = ( a, b ) => String ( a) . localeCompare ( String ( b) ) ;
}
for ( let i = 0 ; i < this . length - 1 ; i++ ) {
for ( let j = i + 1 ; j < this . length; j++ ) {
if ( compareFunction ( this [ i] , this [ j] ) > 0 ) {
[ this [ i] , this [ j] ] = [ this [ j] , this [ i] ] ;
}
}
}
return this ;
} ;
let arr = [ 3 , 1 , 4 , 1 , 5 , 9 ] ;
console. log ( arr. mySort ( ) ) ;