Home > Web Front-end > JS Tutorial > How Can I Efficiently Remove Duplicate Objects from an Array in JavaScript?

How Can I Efficiently Remove Duplicate Objects from an Array in JavaScript?

DDD
Release: 2024-12-21 19:53:12
Original
218 people have browsed it

How Can I Efficiently Remove Duplicate Objects from an Array in JavaScript?

Removing Duplicates from an Array of Objects: A Detailed Exploration

This article delves into a common problem faced when working with arrays of objects: removing duplicate entries. Our aim is to provide an in-depth understanding of the best methods to achieve this task.

Problem Statement

Consider the following object:

obj = {};
obj.arr = new Array();
obj.arr.push({place: "here", name: "stuff"});
obj.arr.push({place: "there", name: "morestuff"});
obj.arr.push({place: "there", name: "morestuff"});
Copy after login

Our goal is to remove duplicate objects from obj.arr so that it contains only unique elements.

ES6 Magic

Leveraging the power of ES6, we can use a one-liner solution:

obj.arr = obj.arr.filter((value, index, self) =>
  index === self.findIndex((t) => (
    t.place === value.place && t.name === value.name
  ))
);
Copy after login

Generic Approach

For a more versatile solution, consider the following code:

const uniqueArray = obj.arr.filter((value, index) => {
  const _value = JSON.stringify(value);
  return index === obj.arr.findIndex(obj => {
    return JSON.stringify(obj) === _value;
  });
});
Copy after login

Property Strategy

An alternative approach is to compare the properties of the objects:

const isPropValuesEqual = (subject, target, propNames) =>
  propNames.every(propName => subject[propName] === target[propName]);

const getUniqueItemsByProperties = (items, propNames) => 
  items.filter((item, index, array) =>
    index === array.findIndex(foundItem => isPropValuesEqual(foundItem, item, propNames))
  );
Copy after login

Explanation

The provided solutions utilize the following concepts:

  • filter: Selects array elements that meet a specified condition.
  • findIndex: Returns the index of the first element in the array that meets the condition.
  • Property comparison: Determines whether two objects have identical values for a set of specified properties.

By employing these strategies, we can effectively remove duplicate objects from an array, ensuring that each element is unique.

The above is the detailed content of How Can I Efficiently Remove Duplicate Objects from an Array in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template