📅  最后修改于: 2022-03-11 15:03:01.952000             🧑  作者: Mango
const lst = [[0, 0], [5, 4], [3, 1]];
const center = [1, 2];
const k = 2;
const findNearestPoints = ({lst, center, k}) => {
// I assume the data are valid; no error checks
const calcHypo = x => Math.sqrt((x[0] - center[0])**2
+ (x[1] - center[1])**2);
const sortPoints = (a,b) => calcHypo(a) - calcHypo(b);
return lst
.sort(sortPoints)
.slice(0,k);
};
console.log(findNearestPoints({lst, center, k}));