Hello Everyone,
I have an array of several eponymous occurrences of two (for example) definitions. How do I remove all duplicates with the same object name from the array? Aristata.Unique does not work in this case. Tell me please, is there some standard simple algorithm for such a case?
For example:
I have
[def1occ1,def1occ2,def1occ3]
Need
[def1occ<any>]
OR
I have
[def1occ1,def1occ2,def1occ3,def2occ1,def2occ2,def2occ3];
Need
[def1occ<any>, def2occ<any>];
I came up with this algorithm. Is there a simpler and more compact version?
/*documentsPackage - an array with the same occurrences of one or more
definitions, from which duplicates must be removed*/
for (var k=0;k<documentsPackage.length;k++){
documentsNamesPackage[k] = documentsPackage[k].ObjDef().Name(gl);
}
for (var l=0;l<documentsNamesPackage.length;l++){
var count = 0;
for (var n=0;n<documentsPackage.length;n++){
if (documentsNamesPackage[l] == documentsPackage[n].ObjDef().Name(gl) && count == 0){
count++;
continue;
} else if (documentsNamesPackage[l] == documentsPackage[n].ObjDef().Name(gl) && count != 0){
documentsPackage.splice(n,n);
}
}
}
Hi,
could be something like this:
mCheckMap = new java.util.Hashtable();
for (i = 0; i < oObjOccList.length; i++){
oObjOcc = oObjOccList[i];
if (!mCheckMap.containsKey(oObjOcc.ObjDef().GUID())){
mCheckMap.put(oObjOcc.ObjDef().GUID(), "");
aoNewArray.push(oObjOcc)
}
}
But perhaps you should go to ObjDef level instead from the beginning...
BR Robert
Robert Goldenbaum, thank you very much!!
Another solution, leveraging
- HashSet instead of HashMap since the map-values aren't used for anything useful anyways
- JavaScript array.filter method
function filterOccsByDefinitionGUID(occsToFilter) {
var guidSet = Packages.java.util.HashSet();
function filterOcc(someOcc) {
return guidSet.add(someOcc.ObjDef().GUID());
}
return occsToFilter.filter(filterOcc);
}
var selOccs = ArisData.getSelectedObjOccs();
var result = filterOccsByDefinitionGUID(selOccs);
Hi.
Javascript version
function getFirstObjOccArray(objOccs) {
const firstOccList = objOccs.reduce(function (list, nextObjOcc) {
const guid = nextObjOcc.ObjDef().GUID() + '';
if (!(guid in list)) {
list[guid] = nextObjOcc;
};
return list;
}, {});
return Object.keys(firstOccList).map(function (nextKey) {
return firstOccList[nextKey];
});
} // getFirstObjOccArray