Suppose I have an array containing objects:
var arr = [
{ id: 1, pt: 0 },
{ id: 2, pt: 12 },
{ id: 3, pt: 7 },
{ id: 4, pt: 45 },
{ id: 5, pt: 123 },
];
I am looking to loop through this array (possibly using array.forEach
or array.map
) and compare the pt
attribute of each item with that of the other items in the array. My goal is to identify the three other items with values closest to the current item's pt
value. For instance, for id: 1
, the closest items in value would be 2, 3, and 4. Similarly, for id: 3
, it would be 1, 2, and 4, and so on. How can I achieve this?