My situation is this, I have a Point object thats has an 'x' and 'y' properties. Point also have a getDistance() method that calculates the distance between two Points.
Example:
if (position.getDistance(prevPosition) > 10) {
// add new Point
} else {
// don't add new Point
}
I have an array of Points and I need to check if the new Point is too close to all the other Points in the array. looking at the Javascript documentation the every() method seems to be the go to function to use but I'm struggling implementing it.
function getDistance(position, prevPosition) {
return position.getDistance(prevPosition) > 10;
}
if (points.every(position)) {
// add new Point
} else {
// don't add Point
}
Any help will be much appreciated
.every takes a function, and every element of the array gets passed to that function (in your case, every point gets passed) and then you have to return a predicate (whether it is far away):
points.every(position => position.getDistance(newPoint) > 10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With