How to remove duplicate from array of objects with multiple properties as unique?

foodiepanda

I have below array in jQuery as below.

var originalArray=[
   {ID: "01", PID: "1111", Week: "Week1"},
   {ID: "02", PID: "2222", Week: "Week1"},
   {ID: "03", PID: "3333", Week: "Week1"},
   {ID: "04", PID: "4444", Week: "Week1"},
   {ID: "05", PID: "1111", Week: "Week1"}, //Duplicate entry like ID 01

   {ID: "06", PID: "1111", Week: "Week2"},
   {ID: "07", PID: "2222", Week: "Week2"},
   {ID: "08", PID: "3333", Week: "Week2"},
   {ID: "09", PID: "4444", Week: "Week2"},
   {ID: "10", PID: "1111", Week: "Week2"}, //Duplicate entry like ID 06

   {ID: "11", PID: "1111", Week: "Week3"},
   {ID: "12", PID: "2222", Week: "Week3"},
   {ID: "13", PID: "3333", Week: "Week3"},
   {ID: "14", PID: "4444", Week: "Week3"},
   {ID: "15", PID: "2222", Week: "Week3"}, //Duplicate entry like ID 11

   {ID: "16", PID: "1111", Week: "Week4"},
   {ID: "17", PID: "2222", Week: "Week4"},
   {ID: "18", PID: "3333", Week: "Week4"},
   {ID: "19", PID: "4444", Week: "Week4"},
   {ID: "20", PID: "4444", Week: "Week4"}  //Duplicate entry like ID 19
];

Now, what I want is all the records with combination of only properties "PID" & "Week" as duplicate to be removed.
To explain further, below records should be removed.

  • Record with ID "05" --> PID "1111" & Week "Week1" is same as ID "01"
  • Record with ID "10" --> PID "1111" & Week "Week2" is same as ID "06"
  • Record with ID "15" --> PID "2222" & Week "Week3" is same as ID "11"
  • Record with ID "20" --> PID "4444" & Week "Week4" is same as ID "19"

This is what I have tried so far:

//Array declaration
var originalArray=[
   {ID: "01", PID: "1111", Week: "Week1"},
   {ID: "02", PID: "2222", Week: "Week1"},
   {ID: "03", PID: "3333", Week: "Week1"},
   {ID: "04", PID: "4444", Week: "Week1"},
   {ID: "05", PID: "1111", Week: "Week1"}, //Duplicate entry like ID 01
   
   {ID: "06", PID: "1111", Week: "Week2"},
   {ID: "07", PID: "2222", Week: "Week2"},
   {ID: "08", PID: "3333", Week: "Week2"},
   {ID: "09", PID: "4444", Week: "Week2"},
   {ID: "10", PID: "1111", Week: "Week2"}, //Duplicate entry like ID 06
   
   {ID: "11", PID: "1111", Week: "Week3"},
   {ID: "12", PID: "2222", Week: "Week3"},
   {ID: "13", PID: "3333", Week: "Week3"},
   {ID: "14", PID: "4444", Week: "Week3"},
   {ID: "15", PID: "2222", Week: "Week3"}, //Duplicate entry like ID 11
   
   {ID: "16", PID: "1111", Week: "Week4"},
   {ID: "17", PID: "2222", Week: "Week4"},
   {ID: "18", PID: "3333", Week: "Week4"},
   {ID: "19", PID: "4444", Week: "Week4"},
   {ID: "20", PID: "4444", Week: "Week4"}  //Duplicate entry like ID 19
];

var result = [];
var tempArr = [];

	$.each(originalArray, function (index, entry) {
		if (!(tempArr[entry.PID] && tempArr[entry.Week])) {
			tempArr[entry.PID] = true;
			tempArr[entry.Week] = true;
			result.push(entry);
		}
	});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

With above tried code, I think it only checks first property PID, but not second property Week.
I have also tried below references, but could hardly find any help.

Duplicates in object array
How to remove duplicated OBJECTS from JavaScript array?
Remove duplicates from array of objects - Javascript
JavaScript Array Distinct()

Thanks in advance.

mikado

You can put your custom logic to check for duplicates into a separate function and then iterate the original array and only copy entries when they are not duplicates.

function isDuplicate(entry, arr) {
  return arr.some(x => (entry.PID == x.PID) && (entry.Week == x.Week))
}

let newArray = []
for (const entry of originalArray) {
  if (!isDuplicate(entry, newArray)) { newArray.push(entry) }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove objects with duplicate properties from Swift array

Remove 'duplicate' objects from array by comparing sub properties (typescript)

Remove duplicate from array of objects based on value of properties in JavaScript

How to get unique objects from array of objects by all properties?

How to get an array of unique properties from an array of complex objects

Javascript: Remove elements with a duplicate key-value pair from an array of unique objects except first such one (NOT DUPLICATE)

How to remove duplicate data values from an array of objects in javascript?

How to remove duplicate objects from json array using hashset?

How to remove duplicate objects from an array based on a condition in javascript

remove duplicate values from a list with multiple properties

How to remove properties from multiple objects if its not undefined?

How can i remove all unique objects from an array in Javascript?

How to remove JSON properties with a specific name from an array of objects?

JavaScript: How to remove the duplicate entries from array with respect to multiple keys

Javascript: Remove duplicate objects from array with conditions

Remove duplicate values from an array of objects in javascript

Remove objects with duplicate date property from Array

Remove Duplicate objects from JSON Array

Remove first duplicate from an array of objects in JavaScript

Javascript - Remove duplicate ID from array of objects

remove duplicate from array of objects based on condition

Remove duplicate objects from an array with Keys

How To Group Objects In Array By Unique Properties in Javascript?

Javascript remove array objects with duplicate properties while keeping the latest occurance

Remove duplicate objects in an array but keep add together a few properties in javascript

Merge properties of objects within a array together using values and remove duplicate

How to remove both duplicate values in an array of objects

how to remove first element of duplicate in an array of objects

Remove some properties from array of javascript objects