โ† flaviocopes.com

JavaScript, how to replace an item of an array

Published Nov 18 2020

Psssst! The 2023 WEB DEVELOPMENT BOOTCAMP is starting on FEBRUARY 01, 2023! SIGNUPS ARE NOW OPEN to this 10-weeks cohort course. Learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much more! โœจ

Given an array, if you know the index of an item you can replace its content using a simple assignment:

const items = ['a', 'b', 'c', 'd', 'e', 'f']
const i = 2

items[i] = '--NEW-ITEM--'

console.log(items)
//[ 'a', 'b', '--NEW-ITEM--', 'd', 'e', 'f' ]

If you donโ€™t know the index of the item, you might need to first find the index of the item in the array.



I write books for developers ๐Ÿ‘‡๐Ÿผ

© 2023 Flavio Copes Flavio Copes made in Italy ๐Ÿ‡ฎ๐Ÿ‡น using Notion to Site Notion to Site