Navigate back to the homepage

How to find and remove object from array in javascript?

Infinitbility
JavaScript
May 22nd, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show you how do I find and remove objects from the array, here I will use the javascript 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 an element index value.

  • The 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.

  • The 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().

Well, let’s start today’s topic How to find and remove the object from an array in javascript?

Before going to code we have below things.

First, we have any object property value which we will use to find objects in an array.

The second is an array of objects 😆.

Okay, let’s plan what we are going to do in code.

  1. Create a sample array of objects variable.
  2. To get index, use findIndex() method
  3. write condition to get the index of the desired object
  4. remove an element from an array using the index value
1// Create sample array of objects variable.
2const domains = [
3 {id: 1, name: "infinitbility", domain: "infinitbility.com"},
4 {id: 2, name: "aGuideHub", domain: "aguidehub.com"},
5 {id: 1, name: "SortoutCode", domain: "sortoutcode.com"},
6];
7
8// To get index, use `findIndex()` method
9// write condition to get index of desire object
10let index = domains.findIndex(e => e.id == 2);
11
12console.log("index", index);
13// Expected 'index' 1
14
15// remove element from array using index value
16domains.splice(index, 1)
17
18console.log("domains", domains);
19/* 'domains' [
20 {id: 1, name: "infinitbility", domain: "infinitbility.com"},
21 {id: 1, name: "SortoutCode", domain: "sortoutcode.com"},
22]
23*/

In the above example, I’m using the object id property to get desired object index from the array, you can use any property which unique.

Here, as per expectation, it should show

1'domains' [
2 {id: 1, name: "infinitbility", domain: "infinitbility.com"},
3 {id: 1, name: "SortoutCode", domain: "sortoutcode.com"},
4]

let’s check the output.

javascript, find and remove object from array example
javascript, find and remove object from array example

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.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to find common elements in two arrays javascript?

Example of find common elements in two arrays in javascript with step by step guide.

May 22nd, 2022 · 1 min read

How to find consecutive repeated characters in a string in javascript?

Example of find consecutive repeated characters in a string in javascript with step by step guide.

May 22nd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]