Created
November 7, 2020 16:38
-
-
Save rajatjain-21/9bbc5830998df91763ede739ddf43313 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Method for creation of array | |
function array(...arguments) { | |
let arr = Object.create(array.prototype); | |
Object.defineProperty(arr, "length", { | |
value: 0, | |
enumerable: false, | |
writable: true | |
}) | |
for(let i=0;i<arguments.length;i++) { | |
arr[i] = arguments[i]; | |
arr.length += 1; | |
} | |
return arr; | |
} | |
// push method | |
array.prototype.push = function(element) { | |
this[this.length] = element; | |
this.length+=1; | |
} | |
// pop method | |
array.prototype.pop= function() { | |
this.length -= 1; | |
const returnValue = this[this.length]; | |
delete(this[this.length]); | |
return returnValue; | |
} | |
// filter method | |
array.prototype.filter = function(callback) { | |
let result = array(); | |
for(let index in this) { | |
if(this.hasOwnProperty(index)) { | |
const element = this[index]; | |
if(callback(element, index)) { | |
result.push(element); | |
} | |
} | |
} | |
return result; | |
} | |
const friendsArray = array('Subham', 'Dewang', 'Aman'); | |
console.log(friendsArray) | |
// friendsArray = {0: 'Subham', 1: 'Dewang', 2: 'Aman'} | |
console.log(friendsArray[1]) // Dewang | |
friendsArray.push('Rajat'); | |
console.log(friendsArray); | |
// friendsArray = {0: 'Subham', 1: 'Dewang', 2: 'Aman', '3': 'Rajat'} | |
friendsArray.filter((element, index) => element.includes('m')); | |
// friendsArray = {0: 'Subham', 1: 'Aman'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment