【问题标题】:How can I store the value of the arrays to an individual variable如何将数组的值存储到单个变量
【发布时间】:2022-12-19 15:03:29
【问题描述】:

我有我的这段代码,其中在我存储我的数组的值之前,它们首先被排序并通过 5 个数据点过滤到最接近用户输入的值。这是我的代码:enter image description here

我尝试从 console.log(findClosest) 命令存储它们,但它似乎不起作用

【问题讨论】:

    标签: javascript arrays sorting


    【解决方案1】:

    看起来 findClosest 函数被定义为箭头函数并且没有返回值。箭头函数隐式返回 => 右侧表达式的值,因此您需要显式返回要存储在最近变量中的值。

    const findClosest = (arr, num) => {
      return arr.sort((a, b) => Math.abs(a - num) - Math.abs(b - num)).filter((a, i) => i < 5);
    }

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多