Skip to content

divinestylus/jsArrayMethodAlgorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

JavaScript Array Method Algorithm

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

  • 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"

.push()

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

.shift()

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"

.unshift()

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

.map()

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']

.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