how to replace numbers in an array javascript code example

Example 1: replace specific values in array

var fruits = [“banana”, “apple”, “orange”, “watermelon”, “apple”, “orange”, “grape”, “apple”];
fruits.splice(0, 2, “potato”, “tomato”);
console.log(fruits); // returns [“potato”, “tomato”, “orange”, “watermelon”, “apple”, “orange”, “grape”, “apple”]

Example 2: replace element in array typescript

const index: number = items.indexOf(element);

if (index !== -1) {
    items[index] = newElement;
}