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.
The pop method removes the last item of the array and returns that item.
ALGORITHM
- Create a new empty array
- Loop over current array except the last item
- Assign the looped over item(s) from the current array to the new array
- Overwrite the item(s) of the current array with the item(s) of the new array
- Return the last item of the current array that was added
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"
The push method adds a new item to the end of an array and returns the new length of the array.
ALGORITHM
- Check to see if an item was passed or not
- Add the new item to the end of the array
- Return the length of the array
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
The shift method removes the first item of the array and returns that item.
ALGORITHM
- Create a new empty array
- Loop over current array except the first item
- Assign the looped over item(s) from the current array to the new array
- Overwrite the item(s) of the current array with the item(s) of the new array
- Return the first item of the current array that was removed
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"
The unshift method adds an item to the beginning of the array and returns the new length of the array.
ALGORITHM
- Check to see if an item was passed or not
- Create a new array and assign the new item to it
- Loop over the current array and add the items to the new array
- Overwrite the current array with the new array
- Return the new length of the current array
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
The map method runs a function on each item of the current array and returns the result in a new array.
ALGORITHM
- Create a new array
- Loop over the current array
- Run an IIFE on each item of the current array and add the return value to the new array
- Return the new array
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']
The filter method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The reduce method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The reduceRight method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The slice method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The splice method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The forEach method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The includes method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The find method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The some method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The every method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The sort method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The reverse method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The findIndex method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The indexOf method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The lastindexOf method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The concat method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT
The join method
let arr = ["Montserrado", "Margibi", "Lofa", "Nimba", "Maryland", "Rivercess", "Gbarpolu", "Bomi", "Sinoe"];
RESULT