Replace Item in Array with JavaScript

A short tutorial on how to replace an element in an array with JavaScript.

Replace item in array using the index

One of the most common ways to replace an element in an array is to use its index. If you know the index of the element you want to replace, you can update it.

Don't worry if you don't know the index of the item you want to replace. In the next part, I will show you how to find it.

const books = [
  "You don't know JS", // Index 0
  'Eloquent JavaScript', // Index 1
  'JavaScript: The Good Parts', // Index 2
]

books[2] = 'JavaScript: The Definitive Guide'

console.log(books)
// Output: [
//  "You don't know JS",
//  "Eloquent JavaScript",
//  "JavaScript: The Definitive Guide",
//]

console.log(books[2])
// Output: "JavaScript: The Definitive Guide"

Replace item in array using IndexOf

If you don't know the index of the item you want to replace, you can use the JavaScript indexOf method to find it.

The indexOf function helps you find the book's index in the example below. If the element isn't in the array, the index returned is -1.

const books = [
  "You don't know JS", // Index 0
  'Eloquent JavaScript', // Index 1
  'JavaScript: The Good Parts', // Index 2
]

const indexOfLastBook = books.indexOf('JavaScript: The Good Parts')

console.log(indexOfLastBook)
// ✅ Output: 2

const indexOfFakeBook = books.indexOf('Fake book')

console.log(indexOfFakeBook)
// ❌ Output: -1

To perform an item replacement, you can use the returned index to update the element if it's in the array.

const books = [
  "You don't know JS", // Index 0
  'Eloquent JavaScript', // Index 1
  'JavaScript: The Good Parts', // Index 2
]

const indexOfLastBook = books.indexOf('JavaScript: The Good Parts')

console.log(indexOfLastBook)
// Output: 2

if (indexOfLastBook !== -1)
  books[indexOfLastBook] = 'JavaScript: The Definitive Guide'

console.log(books)
// Output: [
//  "You don't know JS",
//  "Eloquent JavaScript",
//  "JavaScript: The Definitive Guide",
//]

console.log(books[indexOfLastBook])
// Output: "JavaScript: The Definitive Guide"

If you want to have more information, you can read the indexOf documentation.

Replace item in array using Splice

Another way to replace an item in an array is by using the JavaScript splice method.

The splice function allows you to update an array's content by removing or replacing existing elements.

As usual, if you want to replace an item, you will need its index. Here are the parameters you will use with splice:

  1. index of the element to replace
  2. the number of elements to remove after the index (1 in your case)
  3. the value to add

If you read the return of splice, you will see the array's deleted values. In your case, it will be the book at the specified index.

const books = [
  "You don't know JS", // Index 0
  'Eloquent JavaScript', // Index 1
  'JavaScript: The Good Parts', // Index 2
]

// Replace the index 2, 1 element with the value "JavaScript: The Definitive Guide"
const removedBooks = books.splice(2, 1, 'JavaScript: The Definitive Guide')

console.log(removedBooks)
// Output: [ "JavaScript: The Good Parts" ]

console.log(books)
// Output: [
//  "You don't know JS",
//  "Eloquent JavaScript",
//  "JavaScript: The Definitive Guide",
//]

console.log(books[2])
// Output: "JavaScript: The Definitive Guide"

If you want to have more information, you can read the splice documentation.

Find and replace object in array

If you want to replace an object in an array, you can do the same as the previous ways. The main difference is the variable type. For example, you will not treat with strings but with objects. It means that you will need to replace your value with an object.

const books = [
  { id: 0, name: "You don't know JS" }, // Index 0
  { id: 1, name: 'Eloquent JavaScript' }, // Index 1
  { id: 2, name: 'JavaScript: The Good Parts' }, // Index 2
]

books[2] = { id: 2, name: 'JavaScript: The Definitive Guide' }

console.log(books)
// Output: [
//  { id: 0, name: "You don't know JS" },
//  { id: 1, name: "Eloquent JavaScript" },
//  { id: 2, name: "JavaScript: The Definitive Guide" }
//]

console.log(books[2])
// Output: { id: 2, name: "JavaScript: The Definitive Guide" }

If you want to replace an object in an array, you can find its index based on one of its property values. To do that, you can use the JavaScript findIndex method.

The findIndex function returns the index of the first element matching the condition. It also returns -1 if the condition is not met in the array.

const books = [
  { id: 0, name: "You don't know JS" }, // Index 0
  { id: 1, name: 'Eloquent JavaScript' }, // Index 1
  { id: 2, name: 'JavaScript: The Good Parts' }, // Index 2
]

const lastBookIndex = books.findIndex(
  (book) => book.name === 'JavaScript: The Good Parts'
)

console.log(lastBookIndex)
// Output: 2

if (lastBookIndex !== -1)
  books[lastBookIndex] = { id: 2, name: 'JavaScript: The Definitive Guide' }

console.log(books)
// Output: [
//  { id: 0, name: "You don't know JS" },
//  { id: 1, name: "Eloquent JavaScript" },
//  { id: 2, name: "JavaScript: The Definitive Guide" }
//]

console.log(books[lastBookIndex])
// Output: { id: 2, name: "JavaScript: The Good Parts" }

If you want to have more information, you can read the findIndex documentation.


➡️ I'm starting to tweet more consistently. If you want to get more tips and resources about web programming -> Find me on my Twitter 🐦

Learn to code with simple and concrete examples.

English / Français