How To Remove Object From An Array By It Value In JavaScript

Remove object from an array by its value in javascript

This article will give you some methods to remove object from an array by it value in JavaScript. One of them that you can use is the findIndex() function. Let’s go into detail now.

Remove object from an array by it value in JavaScript

Method 1: Using findIndex() and splice()

The findIndex() method will return the index of the first element that matches the condition.

Syntax:

 array.findIndex(function(currentValue, index, arr), thisValue)

Parameters:

  • function(): (Required) This function will be called each time loop an element in the array.
  • currentValue: (Required) Indicate the value of the current element.
  • index: (Optional) The index of the current element.
  • arr: (Optional) Indicate the array of the current element.
  • thisValue : (Optional) Indicate default undefined. A value is passed to the function as this value.

The splice() method can add or remove elements of the array.

Syntax:

 splice(start, amount, element1, element2, elementN)

Parameters:

  • start: Indicate the position where you want to start adding or removing elements. If you pass in a negative number, it will start counting from the end of the array.
  • amount: Indicate the number of elements you want to delete from your array.
  • element1, element2, elementN: The elements that you want to add to your array.

Example:

const anArray = [
  	{ index: 1 }, 
  	{ index: 2 }, 
  	{ index: 3 }
];
 
const findIdxOfObjValue = (array, value) => {
    const indexOfObject = array.findIndex((ele) => {
      	return ele.index == value;
    });

    return indexOfObject;
};

anArray.splice(findIdxOfObjValue(anArray, 2), 1);
console.log(anArray);

Output :

[ { index: 1 }, { index: 3 } ]

Method 2: Using filter()

The filter() method will return the new array filled with the element that passes the condition you provide through the function. In this situation, we will return the array of the object that doesn’t equal with value.

Syntax:

array.filter(function(currentValue, index, array), thisValue)

Parameters:

  • function() : (Required) Indicate a function will be executed for each loop time of the array element
  • currentValue : (Required) Indicate current element’s value.
  • index : (Optional) Indicate current element’s index.
  • thisValue : (Optional) Indicate default undefined. A value is passed to the function as its this value.

Example:

const anArray = [
  	{ index: 1 }, 
  	{ index: 2 }, 
  	{ index: 3 }
];
 
const removeObjectByValue = (array, value) => {
    const filledArray = array.filter((ele) => {
      	return ele.index !== value;
    });

    return filledArray;
};

const filledArray = removeObjectByValue(anArray, 2); 
console.log(filledArray);

Output :

[ { index: 1 }, { index: 3 } ]

Summary

In this tutorial, I have shown and explained two methods to remove the object from an array by its value in JavaScript. You can use findIndex() to find out the object’s index that matches the value, then use the splice() method to remove that index. Or you can use filter() method to make a new array with all origin objects without an object that matches the condition. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *