📋 Table Of Content
To remove a specific element of an array we have to first find the position of the element in the array and then remove it.
To get the position of the specific element we have to know the index of that element in the array. to get the index we will use
Once we know the index of that element , then we can use
splice() to remove it from the array.
splice() method add or remove an element in am array by its index. It takes two argument :
start , which is the index in the array, and
deleteCount, the number of element it will remove from an array from
Now using the
For example, we will remove an item 'orange' from an array of fruits.
Step 1: Get the index of the item 'orange' from the fruits array using
const fruits = ["Banana", "Orange", "Apple", "Mango"]; const index = fruits.indexOf('Orange') console.log(index) // 1
Step 2 : Now we will use the index of the element (here orange) to remove it from the array using
const fruits = ["Banana", "Orange", "Apple", "Mango"]; const index = fruits.indexOf('Orange') const newArr = fruits.splice(index, 1) console.log(fruits)
[ 'Banana', 'Apple', 'Mango' ]
By using this method you can remove object from array by it's property.