[英]How Can I Re-scale An Array To A Range Of Values in Javascript?
I'd like to re-scale a two dimensional array with a function where the min and max input range and min and max output range can be specified.我想使用可以指定最小和最大输入范围以及最小和最大输出范围的函数重新缩放二维数组。 For example, we want to re-scale the values 0 to 8 to 0 to 1.例如,我们想将值从 0 到 8 重新缩放到 0 到 1。
const scale = (num, in_min, in_max, out_min, out_max) => {
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
var array_scaled = orignal_array.map(scale(num, 0, 8, 0, 1));
}
The code produces the following error: ReferenceError: num is not defined该代码产生以下错误:ReferenceError: num is not defined
What is the correct syntax to call the scale function from within map?从地图内调用比例函数的正确语法是什么?
我猜 map 的参数必须是一个函数,即:
var array_scaled = orignal_array.map(num=>scale(num, 0, 8, 0, 1));
Array.prototype.map()
accepts a callback function as its first argument. Array.prototype.map()
接受一个回调函数作为它的第一个参数。
It looks like num
is not defined as the current element of the array to the callback function scale()
看起来num
没有定义为回调函数scale()
的数组的当前元素
Try var array_scaled = orignal_array.map(array_num => scale(array_num, 0, 8, 0, 1));
试试var array_scaled = orignal_array.map(array_num => scale(array_num, 0, 8, 0, 1));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.