Defoe
Defoe

Reputation: 371

Check duplicates latitudes and longitudes

I'm trying to make work this code that I found in some thread here.

var values= [{lat: 12345, lng: 54321},{lat: 98765, lng: 54321}, {lat: 99999, lng: 111111} ]

let seen = new Set();
var hasDuplicates = values.some(function(currentObject) {
    return seen.size === seen.add(currentObject.lng).size;
});
console.log(hasDuplicates)

It says it's true but It's not because it's only checking if the longitude exist by its own not WITH the latitude.

How can I do that?

Upvotes: 0

Views: 58

Answers (1)

xander
xander

Reputation: 1765

Simple solution, convert the currentObject to a string representation and use that as the Set values.

seen.add(currentObject.lat + " " + currentObject.lng)

var values= [{lat: 12345, lng: 54321},{lat: 12345, lng: 54321}, {lat: 99999, lng: 111111} ]

let seen = new Set();
var hasDuplicates = values.some(function(currentObject) {
    return seen.size === seen.add(currentObject.lat + " " + currentObject.lng).size;
});
console.log(hasDuplicates)

Upvotes: 1

Related Questions