获取JavaScript中数组中的最接近的数字

huangapple go评论69阅读模式
英文:

get closest number out of array in array in javascript

问题

const myarr = [[12, 42], [12, 56], [30, 54]];

console.log(colsest_out_of_closest(myarr, [12, 50]));
英文:

I know this is a bit on nonsense but I need to get the closest number out of 2 arrays or:

const myarr = [[12, 42], [12, 56], [30, 54]]

console.log(colsest_out_of_closest(myarr, [12, 50]))

</details>


# 答案1
**得分**: 1

这是您要翻译的代码部分

```javascript
const closest_out_of_closest = (arr, criteria) => {
  const [min, max] = criteria;
  let result, prevDiff = Number.MAX_VALUE;
  arr.forEach((item) => {
    const [localMin, localMax] = item;
    const diff = Math.abs(localMin - min) + Math.abs(localMax - max);
    if (diff < prevDiff) {
      prevDiff = diff;
      result = item;
    }
  });
  return result;
};

const myarr = [[12, 42], [12, 56], [30, 54]];

console.log(closest_out_of_closest(myarr, [12, 50])); // [12, 56]
const closestRange = (arr, [min, max]) =>
  arr.reduce((acc, [lMin, lMax]) =>
    (diff => diff < acc.prev ? { result: [lMin, lMax], prev: diff } : acc)
    (Math.abs(lMin - min) + Math.abs(lMax - max)),
  { result: null, prev: Number.MAX_VALUE }).result;

console.log(closestRange([[12, 42], [12, 56], [30, 54]], [12, 50])); // [12, 56]

我已经为您提供了翻译,不包括代码部分。

英文:

It looks like you want to find the smallest difference between both the min and the max.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const closest_out_of_closest = (arr, criteria) =&gt; {
  const [min, max] = criteria;
  let result, prevDiff = Number.MAX_VALUE;
  arr.forEach((item) =&gt; {
    const [localMin, localMax] = item;
    const diff = Math.abs(localMin - min) + Math.abs(localMax - max);
    if (diff &lt; prevDiff) {
      prevDiff = diff;
      result = item;
    }
  });
  return result;
};

const myarr = [[12, 42], [12, 56], [30, 54]];

console.log(closest_out_of_closest(myarr, [12, 50])); // [12, 56]

<!-- end snippet -->

Here is a reducer version that is less bytes, but still readable:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const closestRange = (arr, [min, max]) =&gt;
  arr.reduce((acc, [lMin, lMax]) =&gt;
    (diff =&gt; diff &lt; acc.prev ? { result: [lMin, lMax], prev: diff } : acc)
    (Math.abs(lMin - min) + Math.abs(lMax - max)),
  { result: null, prev: Number.MAX_VALUE }).result;

console.log(closestRange([[12, 42], [12, 56], [30, 54]], [12, 50])); // [12, 56]

<!-- end snippet -->

答案2

得分: 1

以下是代码部分的内容,不做翻译:

You could check the absolute delta.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
getClosest = (a, b, t) => Math.abs(t - a) < Math.abs(t - b) ? a : b,
getClosestPair = (target, array) => values.reduce((a, b) =>
[0, 1].map(i => getClosest(a[i], b[i], target[i]))
),
values = [[12, 42], [12, 56], [30, 54]],
closest = getClosestPair([12, 50], values);

console.log(closest);

<!-- end snippet -->

英文:

You could check the absolute delta.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
getClosest = (a, b, t) => Math.abs(t - a) < Math.abs(t - b) ? a : b,
getClosestPair = (target, array) => values.reduce((a, b) =>
[0, 1].map(i => getClosest(a[i], b[i], target[i]))
),
values = [[12, 42], [12, 56], [30, 54]],
closest = getClosestPair([12, 50], values);

console.log(closest);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 02:11:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定