// The splice() method can be used to add new items to an array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
// >> ["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
/"removes any number of consecutive elements from anywhere in an array."/
let array = ['today', 'was', 'not', 'so', 'great'];
array.splice(2, 2);
// remove 2 elements beginning with the 3rd element
// array now equals ['today', 'was', 'great']
const arrayToBeFixed = ['a', 'b', 'wrong', 'f']
const removedElements = arrayToBeFixed.splice(2, // Where to insert
1, // number of elements to be removed from arrayToBeFixed
'c', 'd', 'e', // elements to add starting from the insertion index
);
console.log(arrayToBeFixed) // ['a' 'b', 'c', 'd', 'e', 'f']
console.log(removedElements) // ['wrong']
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 ]
let arr = [1,2,3]
// delete elements:
arr.splice(/*starting index*/, /*number of elements to delete*/)
// delete and replace elements:
arr.splice(/*starting index*/, /*number of elements to delete*/, /* value(s) to replace what was deleted */)
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
//insert new element into array at index 2
let removed = myFish.splice(2, 0, 'drum')
// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
/"removes consecutive elements from anywhere in an array and optionally replace with other elements"/
let arr = ['a1','a2','a3',....]
let replaceArr = ['el1','el2',....]
arr.splice(startIdx,steps,'el1','el2',.....)
// selects startIdx to (startIdx + steps) like slice both inclusive and replaces with el1,el2...
// OR
arr.splice(startIdx,steps,[...replaceArr]) // same using spread
// len(replaceArr) need not be equal to steps, replaceArr is even optional