I am looking for a function that would search for the closet 2 elements out of an array of object based on a known object value. The function would return the indexes for the 2 closest elements or a single index if there is a direct match. it would search by the p variable in each element.
(It is safe to assume the p variable will not appear more than once)
var orbit = [ // p is percent
{ p: 0, x: 0, y: 0, z: 1.2 }
{ p: 30, x: 30, y: 100, z: 0.5 }
{ p: 45, x: 100, y: 30, z: 0.7 }
{ p: 75, x: 60, y: 0, z: 1.0 }
{ p: 100, x: 0, y: 0, z: 1.2 }
];
function ValueToIndexes (value) {
return [close1, close2];
};
if the value was 60 it would return [2,3]
if the value was 30 it would return [1]
1 Answer