Home > Web Front-end > JS Tutorial > How to Efficiently Deduplicate Arrays of Objects Based on Specific Properties?

How to Efficiently Deduplicate Arrays of Objects Based on Specific Properties?

Barbara Streisand
Release: 2025-01-01 04:16:11
Original
1053 people have browsed it

How to Efficiently Deduplicate Arrays of Objects Based on Specific Properties?

Efficient Deduplication of Arrays of Objects

Removing duplicate objects from arrays can be crucial for data integrity and performance optimization. This article explores effective methods to eliminate duplicates from arrays containing objects.

Problem:

Consider an object with an array of nested objects. The goal is to remove duplicate objects based on their "place" and "name" properties.

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

Solution:

Method 1: ES6 Filtering with Array.filter and Array.findIndex

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

This method leverages Array.filter and Array.findIndex to iterate through the array and identify duplicates. It returns only the unique objects, preserving both properties.

Method 2: Generic Solution with JSON.stringify

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

This approach compares the JSON representation of objects to detect duplicates. It's a generic solution that can accommodate objects with any property structure.

Method 3: Using Custom Property Equality Comparison

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

This method allows for more customized property comparison. It uses a callback function to determine property equality and returns a unique set of objects based on the specified properties.

Explanation:

The key to deduplication is finding duplicates and excluding them from the result. The findIndex function helps identify the first instance of an object with specific properties, while filter removes duplicates that occur subsequently.

The above is the detailed content of How to Efficiently Deduplicate Arrays of Objects Based on Specific Properties?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template