var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]
const cars = ['farrari', 'Ice Cream'/* It is not an car */, 'tata', 'BMW']
//to remove a specific element
cars.splice(colors.indexOf('Ice Cream'), 1);
//to remove the last element
cars.pop();
const array = [2, 5, 9];
//Get index of the number 5
const index = array.indexOf(5);
//Only splice if the index exists
if (index > -1) {
//Splice the array
array.splice(index, 1);
}
//array = [2, 9]
console.log(array);
// remove element at certain index without changing original
let arr = [0,1,2,3,4,5]
let newArr = [...arr]
newArr.splice(1,1)//remove 1 element from index 1
console.log(arr) // [0,1,2,3,4,5]
console.log(newArr)// [0,2,3,4,5]
var colors = ["red", "blue", "car","green"];
// op1: with direct arrow function
colors = colors.filter(data => data != "car");
// op2: with function return value
colors = colors.filter(function(data) { return data != "car"});
let dailyActivities = ['work', 'eat', 'sleep', 'exercise'];
// remove the last element
dailyActivities.pop();
console.log(dailyActivities); // ['work', 'eat', 'sleep']
// remove the last element from ['work', 'eat', 'sleep']
const removedElement = dailyActivities.pop();
//get removed element
console.log(removedElement); // 'sleep'
console.log(dailyActivities); // ['work', 'eat']