I have an Array of Geography-Location-Objects of this kind:
let markers = [
{
"markerOffset": 10,
"name": "Plantation",
"coordinates": ["9.85804","53.5233"]
},
{
"markerOffset": 10,
"name": "Roasting",
"coordinates": ["29.85804","50.5233"]
},
{
"markerOffset": 10,
"name": "Packaging",
"coordinates": ["29.85804","50.5233"]
},
{
"markerOffset": 10,
"name": "Harbour",
"coordinates": ["21.85804","51.5213"]
},
]
How can I filter the array to remove duplicate objects with the same coordinates (such as element 2 and 3) and only leave one entry with the coordinates.
A Set wont work propably because of the differing name: attribute
>Solution :
Simple compare and build a new array. This doesn’t care about the name attribute and just uses the first one that comes along
let markers = [
{ "markerOffset": 10, "name": "Plantation", "coordinates": ["9.85804","53.5233"] },
{ "markerOffset": 10, "name": "Roasting", "coordinates": ["29.85804","50.5233"] },
{ "markerOffset": 10, "name": "Packaging", "coordinates": ["29.85804","50.5233"] },
{ "markerOffset": 10, "name": "Harbour", "coordinates": ["21.85804","51.5213"] },
]
let results = [];
markers.forEach(marker => {
if(!results.find(m => m.coordinates[0] === marker.coordinates[0] && m.coordinates[1] === marker.coordinates[1] )) results.push(marker);
})
console.log(results);