I have the following array:
var arr = [{
"id": 1,
"versionID": 1,
"versionNR": 1,
"format": "<div><label for=\"Part_1\">Part 1</label><input data-id=\"1\" id=\"Part_1\" type=\"checkbox\" /></div>"
}, {
"id": 65,
"versionID": 65,
"versionNR": 1,
"format": "<div><label for=\"Part_65\">Part 2</label><input data-id=\"65\" id=\"Part_65\" type=\"checkbox\" /></div>"
}, {
"id": 92,
"versionID": 1,
"versionNR": 2,
"format": "<div><label for=\"Part_92\">Part 1</label><input data-id=\"92\" id=\"Part_92\" type=\"checkbox\" /></div>"
}, {
"id": 93,
"versionID": 65,
"versionNR": 2,
"format": "<div><label for=\"Part_93\">Part 2</label><input data-id=\"93\" id=\"Part_93\" type=\"checkbox\" /></div>"
}, {
"id": 96,
"versionID": 65,
"versionNR": 3,
"format": "<div><label for=\"Part_96\">Part 2</label><input data-id=\"96\" id=\"Part_96\" type=\"checkbox\" /></div>"
}]
var dupes = {};
var singles = [];
$.each(arr, function(i, el) {
if (!dupes[el.versionID]) {
dupes[el.versionID] = true;
singles.push(el);
}
});
console.log(singles)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
What I'm trying to achive is to each unique versionID and return the object with the highest versionNR
My desired result would be:
var arr = [{
"id": 92,
"versionID": 1,
"versionNR": 2,
"format": "<div><label for=\"Part_92\">Part 1</label><input data-id=\"92\" id=\"Part_92\" type=\"checkbox\" /></div>"
},{
"id": 96,
"versionID": 65,
"versionNR": 3,
"format": "<div><label for=\"Part_96\">Part 2</label><input data-id=\"96\" id=\"Part_96\" type=\"checkbox\" /></div>"
}]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
As you can see it the first code Example, it returns correct versionID = 1 & 65, but versionNR is 1 AND not the highest.
I'm suck on getting the result I want. Can someone help me out.