Hi Friends 👋,
Welcome To Infinitbility! ❤️
findIndex() method and some conditions to find the exact object in the array, and the
splice() method to remove a specific element from an array using element index value.
findIndex()method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
splice()method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, see slice().
Before going to code we have below things.
First, we have any property value which we will use to find in an array.
The second is array 😆.
Okay, let’s plan what we are going to do in code.
- Create a sample array variable.
- To get index, use
- write condition to get the index of desire element
- remove an element from an array using an index value
1// Create sample array of objects variable.2const domains = ["infinitbility", "aGuideHub", "SortoutCode"];34// To get index, use `findIndex()` method5// write condition to get index of desire element6let index = domains.findIndex(e => e == "aGuideHub");78console.log("index", index);9// Expected 'index' 11011// remove element from array using index value12domains.splice(index, 1)1314console.log("domains", domains);15// 'domains' [ 'infinitbility', 'SortoutCode' ]
In the above example, I’m using
findIndex() and comparing every element to get desired element index from the array.
Here, as per expectation, it should show
'domains' [ 'infinitbility', 'SortoutCode' ], let’s check the output.
I hope it’s help you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.