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"]
var myArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
//removing element using splice method --
//arr.splice(index of the item to be removed, number of elements to be removed)
//Here lets remove Sunday -- index 0 and Monday -- index 1
myArray.splice(0,2)
//using filter method
let itemToBeRemoved = ["Sunday", "Monday"]
var filteredArray = myArray.filter(item => !itemToBeRemoved.includes(item))
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 fruit = ['apple', 'banana', 'orange', 'lettuce'];
// ^^ An example array that needs to have one item removed
fruit.splice(3, 1); // Removes an item in the array using splice() method
// First argument is the index of removal
// Second argument is the amount of items to remove from that index and on
var colors = ["red", "orange", "yellow", "green"];
colors = colors.splice(3, 1); // Starts removing from the third element, and deletes 1 element
// Array.splice(startingElement, elementYouWantToRemove);
let numbers = [1,2,3,4]
// to remove last element
let lastElem = numbers.pop()
// to remove first element
let firstElem = numbers.shift()
// both these methods modify array while returning the removed element
pop - Removes from the End of an Array.
shift - Removes from the beginning of an Array.
splice - removes from a specific Array index.
filter - allows you to programatically remove elements from an Array.
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']