This code is supposed to concat two elements inside of an Array without duplicating.
The data.CamData contains multiple objects, each object has a CamID, Status, and other data.
each (e) looks like this {Date: 1656533249379, State: 0}
for (let i = 0; i < data.CamData.length; i++) {
let hashSet = new Set();
data.CamData[i].Events.forEach((e) => hashSet.add(e));
state.camData[i].Events.forEach((e) => hashSet.add(e));
data.CamData[i].Events = Array.from(hashSet);
}
However, this code is just concatenating the two without getting rid of duplicates
I believe this is because even though the objects contain similar data they are the different objects. is there any other solution to this?
I think you can use Map or Object, you need to pick a key from your object, for instance 'Date' and set value by key
for (let i = 0; i < data.CamData.length; i++) {
let hashMap = new Map();
data.CamData[i].Events.forEach((e) => hashMap.set(e.Date, e));
state.camData[i].Events.forEach((e) => hashMap.set(e.Date,e));
data.CamData[i].Events = Array.from(hashMap.values());
}
Hope this helps.