Hi everyone! I'm the author of code.mu :)
I'd love to chat with my non-Russian audience. I'm looking for your feedback on the site and the translation quality. Let's chat:)
⊗jsSpDtECh 287 of 294 menu
◀ ▶

Immutable Array Element Modification in JavaScript

Let's now learn how to perform immutable modification of elements in an array. For example, let's say we have some array:

let arr = [1, 2, 3, 4, 5];

Let the index for modification be stored in variable ind:

let ind = 3;

Let's modify the element at the given index. According to our approach, we should make a copy of the array and modify the element in the copy. Let's do this:

let copy = Object.assign([], arr); copy[ind] = '!'; let res = copy;

Let's use the second approach:

let res = [ ...arr.slice(0, ind), '!', ...arr.slice(ind + 1) ];

Create a button that, when clicked, will modify an array element. Let the index of the element to be modified be stored in a variable.

← →
↑
English▼
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline