Remove duplicates from array comparing the properties of its objects
Tag : iphone , By : DicksGarage
Date : March 29 2020, 07:55 AM
should help you out You can create an NSMutableSet of dates, iterate your event list, and add only events the date for which you have not encountered before. NSMutableSet *seenDates = [NSMutableSet set];
NSPredicate *dupDatesPred = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind) {
Event *e = (Event*)obj;
BOOL seen = [seenDates containsObject:e.date];
if (!seen) {
[seenDates addObject:e.date];
}
return !seen;
}];
NSArray *events = ... // This is your array which needs to be filtered
NSArray *filtered = [events filteredArrayUsingPredicate:dupDatesPred];
|
Comparing 2 arrays of objects and removing the duplicates javascript
Date : March 29 2020, 07:55 AM
should help you out If the array include primitive types you can use indexOf and array#reduce const array1 = [0, 1, 2, 3, 4, 5, 6]
const array2 = [7, 8, 1, 2, 9, 10]
var result = array2.filter((num) => {
return array1.indexOf(num) === -1;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const person1 = [{"name":"a", "id":0},{"name":"A", "id":1},{"name":"B", "id":2},{"name":"C", "id":3},{"name":"D", "id":4},{"name":"E", "id":5},{"name":"F", "id":6}]
const person2 = [{"name":"G", "id":7},{"name":"H", "id":8},{"name":"A", "id":1},{"name":"B", "id":2},{"name":"I", "id":9}, {"name":"J", "id":10}]
var unique = person2.reduce((unique, o) => {
let isFound = person1.some((b) => {
return b.id === o.id;
});
if(!isFound)
unique.push(o);
return unique;
},[]);
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }
|
Find duplicates in arrays of objects and pick one of them comparing their values
Date : March 29 2020, 07:55 AM
will be helpful for those in need You could take a hash table as reference to the objects and reduce the array. var array1 = [{ _id: "5972dbe185c5a00906fbefa7", name: "user1", score: 2.0 }, { _id: "59994cf90b79bc4e8233ed1d", name: "user2", score: 1.5 }, { _id: "59880bbaa329573db1180acd", name: "user3", score: 0.5 }, { _id: "5972fa945ad42b149da788b2", name: "user4", score: 3.0 }],
array2 = [{ _id: "5972dbe185c5a00906fbefa7", name: "user1", score: 1.5 }, { _id: "599d2d8fe9feeb290c699535", name: "user5", score: 1.5 }, { _id: "59880bbaa329573db1180acd", name: "user3", score: 3.0 }, { _id: "8072fa945ad42b149da799cc", name: "user8", score: 2.5 }],
hash = Object.create(null),
result = [array1, array2].reduce(function (r, a) {
a.forEach(function (o) {
if (!hash[o._id]) {
hash[o._id] = o;
r.push(hash[o._id]);
return;
}
hash[o._id].score += o.score;
});
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
|
Comparing two objects in Joi validation (eg. to avoid duplicates)
Date : March 29 2020, 07:55 AM
will help you I'm using Joi to validate a complex form entry. The form asks for two addresses, mainContactAddress and seniorContactAddress. I want to validate them to ensure they aren't the same address. , I ended up writing a custom rule for my extension: {
// Enforce a unique address compared to the senior contact
name: 'mainContact',
validate(params, value, state, options) {
// Format addresses into a comparable string,
// making sure we sort them as the stored version
// is in a different order to the form-submitted one.
const serialize = address =>
Object.values(address)
.sort()
.join(',');
const seniorContactAddress = get(
state.parent,
'seniorContactAddress',
[]
);
if (serialize(seniorContactAddress) === serialize(value)) {
return this.createError(
'address.matchesSenior',
{ v: value },
state,
options
);
} else {
return value;
}
}
}
|
Javascript: Is there anyway to prevent duplicates of objects pushed into arrays?
Date : March 29 2020, 07:55 AM
will help you As the various commenters above have pointed out, the answer to your question is largely dependent upon your requirements. If you need to evaluate only on the basis of a single property-- for instance, name-- it is relatively easy: let data = [
{ name: "a", position: "A", grade: "" },
{ name: "b", position: "b", grade: "" },
{ name: "c", position: "c", grade: "" },
{ name: "d", position: "d", grade: "" },
{ name: "a", position: "A", grade: "" },
{ name: "e", position: "E", grade: "" },
{ name: "c", position: "c", grade: "" }
]
let arr = [];
data.forEach(datum => {
if (!arr.find(item => item.name === datum.name)) {
arr.push(datum);
}
});
console.log(arr);
let data = [
{ name: "a", position: "A", grade: "" },
{ name: "b", position: "b", grade: "" },
{ name: "c", position: "c", grade: "" },
{ name: "d", position: "d", grade: "" },
{ name: "a", position: "A", grade: "" },
{ name: "e", position: "E", grade: "" },
{ name: "c", position: "c", grade: "" }
]
let dataObject = {};
data.forEach(datum => {
if (!Object.hasOwnProperty(datum.name)) {
dataObject[datum.name] = datum;
}
});
console.log(Object.values(dataObject));
let data = [
{ name: "a", position: "A", grade: "" },
{ name: "b", position: "b", grade: "" },
{ name: "c", position: "c", grade: "" },
{ name: "d", position: "d", grade: "" },
{ name: "e", position: "E", grade: "" },
];
let data2 = [
data[1],
data[3],
{ name: "f", position: "F", grade: "" },
data[0],
{ name: "g", position: "G", grade: "" },
]
let arr = [];
function pushData(dataArr) {
dataArr.forEach(datum => {
if (!arr.includes(datum)) {
arr.push(datum);
}
})
}
pushData(data);
pushData(data2);
console.log(arr);
let data = [
{ name: "a", position: "A", grade: "" },
{ name: "b", position: "b", grade: "" },
{ name: "c", position: "c", grade: "" },
{ name: "d", position: "d", grade: "" },
{ name: "a", position: "A", grade: "" },
{ name: "e", position: "E", grade: "" },
{ name: "c", position: "c", grade: "" }
]
let arr = [];
function areDatumsEquivalent(datumA, datumB) {
return (
datumA.name === datumB.name &&
datumA.position === datumB.position &&
datumA.grade === datumB.grade
);
}
data.forEach(datum => {
if(!arr.find(arrDatum => areDatumsEquivalent(datum, arrDatum))) {
arr.push(datum);
}
});
console.log(arr);
let data = [
{ name: "a", position: "A", grade: "" },
{ name: "b", position: "b", grade: "" },
{ name: "c", position: "c", grade: "" },
{ name: "d", position: "d", grade: "" },
{ name: "a", position: "A", grade: "" },
{ name: "e", position: "E", grade: "" },
{ name: "c", position: "c", grade: "" }
]
let arr = [];
data.forEach(datum => {
if(!arr.find(arrDatum => _.isEqual(datum, arrDatum))) {
arr.push(datum);
}
});
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
|