Array methods
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
You need to know how to do 3 things with arrays:
As you go through this lesson, you’ll notice there are many ways to do it in JavaScript. They are all correct. What you need to learn (over time) is to choose the methods you prefer.
For now, your job is to understand how each method works. Don’t try to evaluate which is better. You’ll learn my preferred methods (and why I prefer them) as you go through the course.
You can use indexOf
to find a primitive value in the array.
const index = array.indexOf(thing)
thing
exists, indexOf
returns the index you’re looking for.thing
does not exist indexOf
returns -1
.const fruitBasket = ['apple', 'banana', 'orange', 'pear']
const posOfBanana = fruitBasket.indexOf('banana') // 1 (Second item)
const posOfKiwi = fruitBasket.indexOf('kiwi') // -1 (Not found)
Note: indexOf
only works with primitive values. If you want to find the index of an object, array, or function, you need to use findIndex
. You will read about findIndex
later in the course. We don’t need it now.
You can add items in three ways:
Let’s focus on adding items to the start and end first. We’ll talk about adding items to the middle later in this lesson.
You can use unshift
to add items to the start of an array.
const array = [3, 4, 5]
array.unshift(2)
console.log(array) // [2, 3, 4, 5]
If you want to add more items, you can separate each item with ,
in the unshift
method.
const array = [3, 4, 5]
array.unshift(1, 2)
console.log(array) // [1, 2, 3, 4, 5]
You can add items to the end of an array with push
.
const array = [3, 4, 5]
array.push(6)
console.log(array) // [3, 4, 5, 6]
If you want to add more items, you can separate each item with ,
in the push
method.
const array = [3, 4, 5]
array.push(6, 7)
console.log(array) // [3, 4, 5, 6, 7]
You can remove items in three ways:
Again, we will talk about start and end first. We’ll leave middle for later.
You can use shift
to remove items from the start of an array. This method changes the original array.
const array = [3, 4, 5]
const itemOne = array.shift()
console.log(itemOne) // 3
console.log(array) // [4, 5]
If you want to remove 2 (or more) items, you can use shift
2 (or more) times.
const array = [3, 4, 5]
const itemOne = array.shift()
const itemTwo = array.shift()
console.log(itemOne) // 3
console.log(itemTwo) // 4
console.log(array) // [5]
You can remove an item from the end with pop
.
const array = [3, 4, 5]
const lastItem = array.pop()
console.log(array) // [3, 4]
console.log(lastItem) // 5
If you want to remove 2 (or more) items, you can use pop
2 (or more) times.
const array = [3, 4, 5]
const lastItem = array.pop()
const secondLastItem = array.pop()
console.log(array) // [3]
console.log(lastItem) // 5
console.log(secondLastItem) // 4
splice
methodsplice
lets you add items to any position. It also lets you remove items from any position. Its syntax looks a bit confusing, but it’s super convenient.
Here’s the syntax:
const deletedItems = array.splice(index, deleteCount, itemsToAdd)
index
is the position to start modifying the array.deleteCount
is the number of items you want to delete.itemsToAdd
is items you want to add, each separated by ,
.splice
You can use splice
to add items to the start of an array.
const array = [3, 4, 5]
array.splice(0, 0, 1, 2)
console.log(array) // [1, 2, 3, 4, 5]
Here’s what we did with splice:
0
): Start at index 0.0
): Delete 0 items.You can use splice
to add items to the end of an array. (But it’s easier to use push
).
const array = [3, 4, 5]
array.splice(array.length, 0, 6, 7)
console.log(array) // [3, 4, 5, 6, 7]
Here’s what we did:
array.length
): Start modifying the array at array.length
, which means we start modifying the array AFTER the last item.0
): Delete 0 items.You can also use splice to add items to the middle of an array.
const array = [3, 4, 7]
array.splice(2, 0, 5, 6)
console.log(array) // [3, 4, 5, 6, 7]
Here’s what we did:
2
): Start modifying the array at index 2. This means we start modifying the array between the second and third items.0
): Delete 0 items.splice
You can use splice
to remove items from the start of an array.
const array = [3, 4, 5]
const deleted = array.splice(0, 2)
console.log(deleted) // [3, 4]
console.log(array) // [5]
Here’s what we did:
0
): Start at index 0.2
): Delete 2 items.You can use splice
to remove items from the end of an array.
const array = [3, 4, 5]
const deleted = array.splice(array.length - 2, 2)
console.log(array) // [3]
console.log(deleted) // [4, 5]
Here’s what we did:
array.length - 2
): Modifies the array at index array.length - 2
, which is 3 - 2 = 1
. So we remove items from second item onwards.2
): Delete 2 items.You can also use splice to remove items from the middle of an array.
const array = [3, 4, 9, 8, 5]
const deleted = array.splice(2, 2)
console.log(deleted) // [9, 8]
console.log(array) // [3, 4, 5]
Here’s what we did:
2
): Start modifying the array at index 2. This means we start deleting the third item.2
): Delete 2 items.You can make a copy of an array with slice
. If you use the methods on the new array, you won’t change the original array. (This would be helpful for the exercise below).
const array = [3, 4, 5]
const copy = array.slice()
copy.push(6)
console.log(array) // [3, 4, 5]
console.log(copy) // [3, 4, 5, 6]
The following questions require you to make use of the people
array provided below. To make like easier for you, use slice
to make a copy of the array before answer each question.
Mahatma Gandhi
in this list of people? Use indexOf
.Winston Churchill
and Albert Einstein
to the start of the list.
unshift
splice
Charles Darwin
and Walt Disney
to the end of the list.
push
splice
Pablo Picasso
and Ludwig van Beethoven
after Mahatma Gandhi
. Use splice
.Benjamin Franklin
and Thomas Edison
shift
splice
Napoleon Bonaparte
and Abraham Lincoln
pop
splice
Mahatma Gandhi
with splice
Here’s the people
array:
let people = [
'Benjamin Franklin',
'Thomas Edison',
'Franklin Roosevelt',
'Mahatma Gandhi',
'Napoleon Bonaparte',
'Abraham Lincoln'
]
Mahatma Gandhi
in this list of people?const gandhi = people.indexOf('Mahatma Gandhi')
Winston Churchill
and Albert Einstein
to the start of the list.unshift
splice
// With unshift
const array = people.slice()
array.unshift('Winston Churchill', 'Albert Einstein')
// With splice
const array = people.slice()
array.splice(0, 0, 'Winston Churchill', 'Albert Einstein')
Charles Darwin
and Walt Disney
to the end of the listpush
splice
// With push
const array = people.slice()
array.push('Charles Darwin', 'Walt Disney')
// With splice
const array = people.slice()
array.splice(array.length, 0, 'Charles Darwin', 'Walt Disney')
Pablo Picasso
and Ludwig van Beethoven
after Mahatma Gandhi
. Use splice
.const array = people.slice()
const gandhiIndex = array.indexOf('Mahatma Gandhi')
array.splice(gandhiIndex + 1, 0, 'Pablo Picasso', 'Ludwig van Beethoven')
Benjamin Franklin
and Thomas Edison
shift
splice
// With shift
const array = people.slice()
array.shift()
array.shift()
// With splice
const array = people.slice()
array.splice(0, 2)
Napoleon Bonaparte
and Abraham Lincoln
pop
splice
// With pop
const array = people.slice()
array.pop()
array.pop()
// With splice
const array = people.slice()
array.splice(array.length - 2, 2)
Mahatma Gandhi
with splice
const array = people.slice()
const gandhiIndex = array.indexOf('Mahatma Gandhi')
array.splice(gandhiIndex, 1)