Fix reduce warning for empty arrays

This commit is contained in:
ajv 2018-10-25 16:46:20 +03:00 committed by GitHub
parent 81cddd45a3
commit 91bdab2765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -212,7 +212,7 @@ export function isInRange2D(coord, minCoord, maxCoord) {
export function getClosestInArray(goal, arr, index = false) {
let closest = arr.reduce(function(prev, curr) {
return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
});
}, []);
return index ? arr.indexOf(closest) : closest;
}