最近、配列に対して呼び出すことができる新しい相互運用可能なメソッド Array.prototype.with() がブラウザに追加されました。 この記事では、このメソッドの仕組みと、元の配列を変更せずに配列を更新する方法について説明します。 Array.prototype.with(index, value) の概要 Array.prototype.with(index, value) メソッドは、指定された新しい value に index を設定して、呼び出された配列のコピーを返します。 次の例は、年齢層の配列を示しています。配列の新しいコピーを作成し、第 2 の年齢を 15 から 16 に変更します。 const ages = [10, 15, 20, 25]; const newAges = ages.with(1, 16); console.log(newAges); // [1
![Array.prototype.with による不変の配列の更新 | Blog | web.dev](https://cdn-ak-scissors.b.st-hatena.com/image/square/e734906fd891cf742f6fec086afa0469d1e2e13f/height=288;version=1;width=512/https%3A%2F%2Fweb.dev%2Fstatic%2Fblog%2Farray-with%2Fimage%2Fhero.jpg%3Fhl%3Dja)