[Solved] How to get smallest difference value from an array in Javascript


You can use reduce to create such function.

function getMin(data) {
  return data.reduce(function(r, e, i) {
    let absR = Math.abs(r), absE = Math.abs(e);
    if (absR > absE || i == 0 || (absR == absE && e > r)) r = e
    return r
  })
}

console.log(getMin([1.5, -1, 0, 5, 5.5, -4]))
console.log(getMin([1.5, -1, 5, 5.5, -4]))
console.log(getMin([1, -1, 5, 5.5, -4]))
console.log(getMin([-1, 1, 5, 5.5, -4]))

1

solved How to get smallest difference value from an array in Javascript