Here are some more creative uses for the Array.prototype.with()
method:
- Creating a new array with a different value at a random index. This can be useful when you want to simulate a random change in an array.
const arr = [1, 2, 3, 4, 5];
const randomIndex = Math.floor(Math.random() * arr.length);
const randomValue = Math.floor(Math.random() * 10);
const withRandom = arr.with(randomIndex, randomValue);
console.log(withRandom); // e.g. [1, 2, 7, 4, 5]