Skip to content

Instantly share code, notes, and snippets.

@sorinpav
Created April 20, 2020 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorinpav/c33953a4ce4f21c775d3f6965b86bacb to your computer and use it in GitHub Desktop.
Save sorinpav/c33953a4ce4f21c775d3f6965b86bacb to your computer and use it in GitHub Desktop.
Array Methods in JS
//*********** Array.prototype.map()**********************//
//executes a callback function on each element of the given array and RETURNS A NEW ARRAY
//.map() DOES create a new array.
const array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]
//***********************************************************//
//*********** Array.prototype.forEach()**********************//
//.forEach() DOES NOT create a new array, it returns the same one
// executes a callback function on each element of the given array
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// expected output: "a"
// expected output: "b"
// expected output: "c"
//***********************************************************//
//*********** Array.prototype.filter()**********************//
// DOES CREATE A NEW ARRAY
// Applies a condition to the given array and returns A NEW ARRAY with items satisfying the given condition
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
//***********************************************************//
//*********** Array.prototype.reduce()**********************//
//The reduce() method executes a reducer function (that you provide)
//on each element of the array, resulting in a single output value.
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
//currentValue defaults to 0.
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
//***********************************************************//
//*********** Array.prototype.find()**********************//
//The find() method returns the value of the first element in
//the provided array that satisfies the provided testing function.
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// expected output: 12
//***********************************************************//
//*********** Array.prototype.includes()**********************//
//The includes() method determines whether an array includes a
//certain value among its entries, returning true or false as appropriate.
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// expected output: true
console.log(pets.includes('at'));
// expected output: false
//***********************************************************//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment