Navigate back to the homepage

How to remove an object from an array in javascript

Infinitbility
JavaScript
January 17th, 2022 · 1 min read
keep affilitate

Hello Friends 👋,

Welcome to Infinitbility.

When we are using arrays of objects and we have to remove objects from the array then there are no direct methods to remove them.

Today, we will use javascript findIndex() and splice() methods to remove an object from an array using the object key.

Let take the following sample arrays of objects.

1let users = [
2 { id: 1, name: 'Infinitbility', email: '[email protected]' },
3 { id: 2, name: 'notebility', email: '[email protected]' },
4 { id: 3, name: 'stackbility', email: '[email protected]' },
5];

In the above arrays, we have to remove the 'stackbility' object from the array.

To remove array property we have to first find property index.

To find index in array, javascript provide findIndex() method.

Suppose, we have an id of 'stackbility' now we can easily search in an array using id property.

1let index = users.findIndex((item) => item.id === 3);
2
3console.log('index', index); // 2

Here, when I use findIndex() i got 2 now 2 is our 'stackbility' index value and when we pass this index value in the splice() method it will remove the object from the array.

The following code is a complete example of removing an object from an array.

1let users = [
2 { id: 1, name: 'Infinitbility', email: '[email protected]' },
3 { id: 2, name: 'notebility', email: '[email protected]' },
4 { id: 3, name: 'stackbility', email: '[email protected]' },
5];
6
7let index = users.findIndex((item) => item.id === 3);
8
9console.log('index', index);
10
11if (index >= 0) {
12 users.splice(index, 1);
13}
14
15console.log('users', users);

Don’t forget to check the index >= 0 condition else when the index got -1 it will remove the last object.

Output

Javascript remove object from array example
Javascript remove object from array example

Thanks for reading…

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.

melio affilitate

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 check array contains value in react-native?

React Native, array contains value example.

January 16th, 2022 · 1 min read

How to check android version in react native?

React native get android version example.

January 15th, 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]