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

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

get closest number out of array in array in javascript

问题

  1. const myarr = [[12, 42], [12, 56], [30, 54]];
  2. 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:

  1. const myarr = [[12, 42], [12, 56], [30, 54]]
  2. console.log(colsest_out_of_closest(myarr, [12, 50]))
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. 这是您要翻译的代码部分
  7. ```javascript
  8. const closest_out_of_closest = (arr, criteria) => {
  9. const [min, max] = criteria;
  10. let result, prevDiff = Number.MAX_VALUE;
  11. arr.forEach((item) => {
  12. const [localMin, localMax] = item;
  13. const diff = Math.abs(localMin - min) + Math.abs(localMax - max);
  14. if (diff < prevDiff) {
  15. prevDiff = diff;
  16. result = item;
  17. }
  18. });
  19. return result;
  20. };
  21. const myarr = [[12, 42], [12, 56], [30, 54]];
  22. console.log(closest_out_of_closest(myarr, [12, 50])); // [12, 56]
  1. const closestRange = (arr, [min, max]) =>
  2. arr.reduce((acc, [lMin, lMax]) =>
  3. (diff => diff < acc.prev ? { result: [lMin, lMax], prev: diff } : acc)
  4. (Math.abs(lMin - min) + Math.abs(lMax - max)),
  5. { result: null, prev: Number.MAX_VALUE }).result;
  6. 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 -->

  1. const closest_out_of_closest = (arr, criteria) =&gt; {
  2. const [min, max] = criteria;
  3. let result, prevDiff = Number.MAX_VALUE;
  4. arr.forEach((item) =&gt; {
  5. const [localMin, localMax] = item;
  6. const diff = Math.abs(localMin - min) + Math.abs(localMax - max);
  7. if (diff &lt; prevDiff) {
  8. prevDiff = diff;
  9. result = item;
  10. }
  11. });
  12. return result;
  13. };
  14. const myarr = [[12, 42], [12, 56], [30, 54]];
  15. 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 -->

  1. const closestRange = (arr, [min, max]) =&gt;
  2. arr.reduce((acc, [lMin, lMax]) =&gt;
  3. (diff =&gt; diff &lt; acc.prev ? { result: [lMin, lMax], prev: diff } : acc)
  4. (Math.abs(lMin - min) + Math.abs(lMax - max)),
  5. { result: null, prev: Number.MAX_VALUE }).result;
  6. 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);

  1. 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);

  1. 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:

确定