splice() methods :
-- splice(starting index , deleting index , update/delete element)
const months = ['Jan','march','April','June','July'];
console.log("Old array :",months);
//1. add 'Dec' month at last
months.splice(months.length,0,"Dec")
console.log("Dec month added ",months);
//2. update 'march' to 'March'
const indexOfMarch = months.indexOf("march");
if(indexOfMarch != -1){
months.splice(indexOfMarch,indexOfMarch,"March")
console.log('Month value updated: ' , months);
}
else {
console.log('No such data found');
}
//3. delete June month from array
const indexOfJune = months.indexOf("June");
months.splice(indexOfJune,1)
console.log("June month deleted ",months);
//3. delete all elemets after March
// const indexOfApril = months.indexOf("March");
months.splice(indexOfMarch+1,Infinity)
console.log("Delete all element after march ",months);
0 Comments