My Docs
Array-Methods-Lesson
Array-Methods-Lesson
  • Home
    • General Questions
    • Lesson Plan
    • Advice
  • ๐Ÿ‘จ๐Ÿ’ป Methods
    • ๐Ÿ”—Array.forEach()
      • forEach-Questions
    • ๐ŸšฎArray.filter()
      • Filter Questions
    • ๐Ÿ’ฉArray.reduce()
      • Reduce Questions
    • ๐Ÿ—บ๏ธArray.map()
      • Map Questions
    • Native Implementation
  • Background
    • ๐Ÿ˜ถMutability
    • ๐ŸผArray Basics
    • ๐Ÿ”™Background
    • ๐Ÿ“ฒCallback Functions
    • ๐Ÿ”๏ธHigher Order Functions
    • ๐Ÿง˜Difference Between Functions & Methods...
    • โžก๏ธFat Arrow Syntax
    • ๐Ÿ’ฑDoes It Mutateยฟ
    • First Class Functions
  • ๐Ÿ“–Resources
    • ๐Ÿ“ฐCheat Sheet
      • ๐Ÿ‘ทโ€โ™€๏ธ๐Ÿ‘ทโ™€ ๐Ÿ‘ทโ™€ ๐Ÿ‘ทโ™€ ๐Ÿ‘ทโ™€ Examples
    • ๐Ÿค“Other Array Methods
    • Examples
      • Refactor
  • ๐Ÿ‘ฝMiscellaneous
    • ๐ŸArray Methods Explained As Emojis
Powered by GitBook
On this page
  • DOWNLOAD JS FILES FOR LESSON:
  • Table of contents
  • Methods
  • ๐Ÿ‘จ๐Ÿ’ป Methods
  • Aux Information
  • Background
  • ๐Ÿ“– Resources
  • ๐Ÿ‘ฝ Miscellaneous

Was this helpful?

Edit on GitHub

Home

Github Repo: https://github.com/bgoonz/Array-Methods-Lesson

NextGeneral Questions

Last updated 3 years ago

Was this helpful?

map([๐ŸŒฝ, ๐Ÿฎ, ๐Ÿ”], cook) => [๐Ÿฟ, ๐Ÿ”, ๐Ÿณ]

filter([๐Ÿฟ, ๐Ÿ”, ๐Ÿณ], isVegetarian) => [๐Ÿฟ, ๐Ÿณ]

reduce([๐Ÿฟ, ๐Ÿณ], eat) => ๐Ÿ’ฉ

DOWNLOAD JS FILES FOR LESSON:

map([๐ŸŒฝ, ๐Ÿฎ, ๐Ÿ”], cook) => [๐Ÿฟ, ๐Ÿ”, ๐Ÿณ]

filter([๐Ÿฟ, ๐Ÿ”, ๐Ÿณ], isVegetarian) => [๐Ÿฟ, ๐Ÿณ]

reduce([๐Ÿฟ, ๐Ÿณ], eat) => ๐Ÿ’ฉ

Table of contents

Methods

๐Ÿ‘จ๐Ÿ’ป Methods

Aux Information

Background

๐Ÿ“– Resources

๐Ÿ‘ฝ Miscellaneous

// Mutating
push()      // Insert an element at the end
pop()       // Remove an element from the end
unshift()   // Inserts an element in the beginning
shift()     // Remove first element
// Iterating
forEach()   // Iterates an array
filter()    // Iterates an array and result is filtered array
map()       // Iterates an array and result is new array
reduce()    // "Reduces" the array into single value (accumulator)
// Others
slice()     // Returns desired elements in a new array
concat()    // Append one or more arrays with given array

Cheat sheet: Arrays

JavaScript Arrays are a very flexible data structure and used as lists, stacks, queues, tuples (e.g. pairs), etc. Some

Using Arrays

Creating Arrays, reading and writing elements:

const arr = ['a', 'b', 'c']; // Array literal
assert.deepEqual(
  arr,
  [
    'a',
    'b',
    'c', // trailing commas are ignored
  ]
);
assert.equal(
  arr.length, 3 // number of elements
);
assert.equal(
  arr[0], 'a' // read (negative indices donโ€™t work)
);
assert.equal(
  arr.at(-1), 'c' // read (negative indices work)
);
arr[0] = 'x'; // write
assert.deepEqual(
  arr, ['x', 'b', 'c']
);

The lengths of Arrays, adding elements:

const arr = ['a'];
assert.equal(
  arr.length, 1 // number of elements
);
arr.push('b'); // add an element (preferred)
assert.deepEqual(
  arr, ['a', 'b']
);
arr[arr.length] = 'c'; // add an element (alternative)
assert.deepEqual(
  arr, ['a', 'b', 'c']
);
arr.length = 1; // remove elements
assert.deepEqual(
  arr, ['a']
);

Concatenating Arrays via spreading (...):

const arr1 = ['a', 'b'];
const arr2 = ['c'];
const arr3 = ['d', 'e', 'f'];
assert.deepEqual(
  [...arr1, ...arr2, ...arr3, 'g'],
  ['a', 'b', 'c', 'd', 'e', 'f', 'g']
);

Clearing Arrays (removing all elements):

// Affects everyone referring to the Array
const arr1 = ['a', 'b', 'c'];
arr1.length = 0;
assert.deepEqual(
  arr1, []
);

// Does not affect others referring to the Array
let arr2 = ['a', 'b', 'c'];
arr2 = [];
assert.deepEqual(
  arr2, []
);

Looping over elements:

const arr = ['a', 'b', 'c'];
for (const value of arr) {
  console.log(value);
}

// Output:
// 'a'
// 'b'
// 'c'

Looping over key-element pairs:

const arr = ['a', 'b', 'c'];
for (const [index, value] of arr.entries()) {
  console.log(index, value);
}

// Output:
// 0, 'a'
// 1, 'b'
// 2, 'c'

Creating and filling Arrays when we canโ€™t use Array literals (e.g. because we donโ€™t know their lengths in advance or they are too large):

const four = 4;

// Empty Array that weโ€™ll fill later
assert.deepEqual(
  new Array(four),
  [ , , , ,] // four holes; last comma is ignored
);

// An Array filled with a primitive value
assert.deepEqual(
  new Array(four).fill(0),
  [0, 0, 0, 0]
);

// An Array filled with objects
// Why not .fill()? Weโ€™d get single object, shared multiple times.
assert.deepEqual(
  Array.from({length: four}, () => ({})),
  [{}, {}, {}, {}]
);

// A range of integers
assert.deepEqual(
  Array.from({length: four}, (_, i) => i),
  [0, 1, 2, 3]
);

Array methods

Deriving a new Array from an existing Array:

> ['โ– ','โ—','โ–ฒ'].slice(1, 3)
['โ—','โ–ฒ']
> ['โ– ','โ—','โ– '].filter(x => x==='โ– ') 
['โ– ','โ– ']

> ['โ–ฒ','โ—'].map(x => x+x)
['โ–ฒโ–ฒ','โ—โ—']
> ['โ–ฒ','โ—'].flatMap(x => [x,x])
['โ–ฒ','โ–ฒ','โ—','โ—']

Removing an Array element at a given index:

const arr = ['โ– ','โ—','โ–ฒ'];

assert.deepEqual(
  arr.filter((_, index) => index !== 1),
  ['โ– ','โ–ฒ']
);
assert.deepEqual(
  arr, ['โ– ','โ—','โ–ฒ'] // .filter() is non-destructive
);

arr.splice(1, 1); // start at 1, delete 1 element
assert.deepEqual(
  arr, ['โ– ','โ–ฒ'] // .splice() is destructive
);

Computing a summary of an Array:

> ['โ– ','โ—','โ–ฒ'].some(x => x==='โ—')
true
> ['โ– ','โ—','โ–ฒ'].every(x => x==='โ—')
false

> ['โ– ','โ—','โ–ฒ'].join('-')
'โ– -โ—-โ–ฒ'

> ['โ– ','โ—'].reduce((result,x) => result+x, 'โ–ฒ')
'โ–ฒโ– โ—'
> ['โ– ','โ—'].reduceRight((result,x) => result+x, 'โ–ฒ')
'โ–ฒโ—โ– '

Changing all of an Array (the input Array is modified and returned):

> ['โ– ','โ—','โ–ฒ'].fill('โ—')
['โ—','โ—','โ—']
> ['โ– ','โ—','โ–ฒ'].reverse()
['โ–ฒ','โ—','โ– ']
> ['โ– ','โ—','โ– '].sort()
['โ– ','โ– ','โ—']

Finding Array elements:

> ['โ– ','โ—','โ– '].includes('โ– ')
true
> ['โ– ','โ—','โ– '].indexOf('โ– ')
0
> ['โ– ','โ—','โ– '].lastIndexOf('โ– ')
2
> ['โ– ','โ—','โ– '].find(x => x==='โ– ')
'โ– '
> ['โ– ','โ—','โ– '].findIndex(x => x==='โ– ')
0

Listing elements (Array.from() is needed because the methods return iterables, not Arrays):

> Array.from(['โ– ','โ—','โ–ฒ'].keys())
[0,1,2]
> Array.from(['โ– ','โ—','โ–ฒ'].entries())
[[0,'โ– '],[1,'โ—'],[2,'โ–ฒ']]

Adding or removing an element at either end of an Array:

const arr1 = ['โ– ','โ—'];
arr1.push('โ–ฒ');
assert.deepEqual(
  arr1, ['โ– ','โ—','โ–ฒ']
);

const arr2 = ['โ– ','โ—','โ–ฒ'];
arr2.pop();
assert.deepEqual(
  arr2, ['โ– ','โ—']
);

const arr3 = ['โ– ','โ—'];
arr3.unshift('โ–ฒ');
assert.deepEqual(
  arr3, ['โ–ฒ','โ– ','โ—']
);

const arr4 = ['โ–ฒ','โ– ','โ—'];
arr4.shift();
assert.deepEqual(
  arr4, ['โ– ','โ—']
);

Home
Page 1
Array.forEach()
Array.filter()
Array Methods Explained As Emojis
Array.reduce()
Array.map()
Mutability
Array Basics
๐Ÿ”™ Background
Callback Functions
Higher Order Functions
๐Ÿง˜ Difference Between Functions & Methods...
โžก Fat Arrow Syntax
Array Methods Explained In Emojis
Does It Mutateยฟ
Cheat Sheet
Other Array Methods
Array Methods Explained As Emojis
6KB
array-methods.zip
archive
download-js-files
GitHub - bgoonz/Array-Methods-LessonGitHub
Corresponding Github Repository
Page cover image
Logo
HomeMy Docs
Public Web Address
Logo