javascript array replace code example

Example 1: replace array element javascript

// Replace 1 array element at index with item 
arr.splice(index,1,item);

Example 2: javascript replace array element

array.splice(array.indexOf(valueToReplace), 1, newValue);

Example 3: splice javascript

const numbers = [10, 11, 12, 12, 15];
const startIndex = 3;
const amountToDelete = 1;

numbers.splice(startIndex, amountToDelete, 13, 14);
// the second entry of 12 is removed, and we add 13 and 14 at the same index
console.log(numbers);
// returns [ 10, 11, 12, 13, 14, 15 ]

Tags:

Java Example