Array Methods Flashcards
What index is the ‘front’ of an array?
0 (the left-hand side)
What index is the back of an array?
length - 1 (the right-hand side)
(because length is not zero based, where indices are!)
How do I add something to the front (or start) of an array?
UNSHIFT
(So called because it’s the reverse of ‘shifting’)
const newLengthOfArray = myArray.unshift();
How do I remove something from the front (or start) of an array?
SHIFT
(so called because if you remove the first item all the others have to shift along one place towards the front)
const removed = myArray.shift();
How do I put something on to the back (or end) of the array?
PUSH
(You’re pushing something into that list)
myArray.push(thing);
The method returns the new length of the array
const newArrayLength = myArray.push(thing);
How do I remove something from the back (or end) of an array?
POP
myArray.pop();
How do I remove thing[s] from the middle of an array?
What arguments does it take?
What does it return?
SPLICE
splice(startIndex, deleteCount)
returns an array of whatever it deletes
myArray.splice(2, 2); // removes the third and fourth item, so [item3, item3]
How do I insert things into an array?
SPLICE (again)
splice(startIndex, deleteCount, [thingToBeInserted1, thingToBeInserted2,etc…])
myArray.splice(2, 0, 5, 6); // Insert 5 and 6 at index 2
How do I copy parts of an array?
SLICE
myArr.slice() - copies all
myArr.slice(2) - copies from index 2 until the end
myArray.slice(2,4) - copies the things at index 2 and 3 (but not 4!) into a new array
How do I make an array run in the opposite order
REVERSE
myArr.reverse():
(mutates the array in place AND returns it)
(You may encounter issues of ‘lazy evaluation’ when logging. Log the original array then reverse it and log it again, but for both log: JSON.parse(JSON.stringify(myArr)) - this stops that issue)
How do I join 2 arrays together (to make a new array)?
CONCAT
const newArr = arr1.concat(arr2);
arr1’s items will be first in the new array
How do I turn an array into a string?
JOIN
myArr.join()
[‘tim’, ‘jon’, ‘dave’.join(); // tim,jon,dave
[‘tim’, ‘jon’, ‘dave’.join(‘, ‘); // tim, jon, dave
How do I iterate over an array? And what arguments am I given when I do?
(To ‘iterate’ means to go over the array one item at a time)
FOREACH
(you’ll be given: the current item; its index in the array, and; the array itself)
myArr.forEach(function(item, index, array){
// Do something with the items here…
})
How do I find out if my array has a specific item in it? (for primitives and objects I have direct access to)
What arguments do you pass to it?
What does it return?
Is it handled differently for primitives vs custom objects, arrays, etc.
INCLUDES
You pass the item you’re looking for and [optionally] a startIndex, which is where the search will begin from.
It returns a boolean (true or false)
For primitives: [1,2,3].includes(2); // will work
[{ name: ‘James’ }].includes({ name: ‘James’ }); // won’t work
const james = { name: ‘James’ };
[james].includes(james); // will work
How do I find where something is in an array? (for primitives and objects I have direct access to)
What do I pass to it? What does it return?
INDEXOF
You pass the item you’re looking for and [optionally] a startIndex, which is where the search will begin from.
It returns the index of the item or -1
[1,2,3].indexOf(2); // 1
[1,2,3].indexOf(5); // -1