// 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 ( ͡~ ͜ʖ ͡°)
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]
months.splice(0, 1);
// removes 1 element at index 0
console.log(months);
// expected output: Array ["Feb", "March", "April", "May"]
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 arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
var arrayElements = [1,2,3,4,2];
console.log(arrayElements);
//[1, 2, 3, 4, 2]
arrayElements.forEach((element,index)=>{
if(element==2) arrayElements.splice(index,1);
});
console.log(arrayElements);
//[1, 3, 4]