Have you ever wondered how Array methods work under the hood? Do you wish to know what algorithms they use to work their magic? Well I have, and if you’re are like me then you have too. So I decided to recreate them to discover what they are actually made of. Please leave a star on this repository if you find this helpful in any way.
.pop()
The pop method removes the last item of the array and returns that item.
ALGORITHM
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
const pop = array => {
let newArray = [];
for (let i=0; i<array.length -1; i++){
newArray[i] = array[i];
}
arr = newArray;
return array[array.length -1];
}
pop(arr);
RESULT
"Sinoe"
.push()
The push method adds a new item to the end of an array and returns the new length of the array.
ALGORITHM
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
const push = (array, item) => {
if (item !== undefined) {
array[array.length] = item;
}
return array.length;
}
push(arr, "Grand Kru");
RESULT
10
.shift()
The shift method removes the first item of the array and returns that item.
ALGORITHM
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
const shift = (array) => {
let newArray = [];
for (let i=1; i<array.length; i++){
newArray[i -1] = array[i];
}
arr = newArray;
return array[0];
}
shift(arr);
RESULT
"Montserrado"
.unshift()
The unshift method adds an item to the beginning of the array and returns the new length of the array.
ALGORITHM
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
const unshift = (array, item) => {
if (item !== undefined){
let newArray = [item];
for (let i=0; i<array.length; i++){
newArray[i+1] = array[i];
}
arr = newArray;
}
return arr.length;
}
unshift(arr, "Grand Kru");
RESULT
10
.map()
The map method runs a function on each item of the current array and returns the result in a new array.
ALGORITHM
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
const map = array => {
let newArray = [];
for (let i=0; i<array.length; i++){
newArray[i] = function() {
return array[i] + " County";
}();
}
return newArray;
}
map(arr);
RESULT
['Montserrado County', 'Margibi County', 'Lofa County', 'Nimba County', 'Maryland County', 'Rivercess County', 'Gbarpolu County', 'Bomi County', 'Sinoe County']
.filter()
The filter method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.reduce()
The reduce method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.reduceRight()
The reduceRight method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.slice()
The slice method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.splice()
The splice method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.forEach()
The forEach method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.includes()
The includes method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.find()
The find method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.some()
The some method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.every()
The every method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.sort()
The sort method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.reverse()
The reverse method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.findIndex()
The findIndex method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.indexOf()
The indexOf method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.lastIndexOf()
The lastindexOf method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.concat()
The concat method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
.join()
The join method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT