如何规范化 JavaScript 中的正数列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13368046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to normalize a list of positive numbers in JavaScript?
提问by Saatana
I have an array filled with positive int values, how could I normalize this list so the max value is always 100? Thank you in advance!
我有一个填充了正整数值的数组,如何规范化这个列表,使最大值始终为 100?先感谢您!
回答by Joseph Silber
The idea is to first find the highest number in your array (using apply
on Math.max
), then find the ratio between that highest number and 100.
这个想法是首先找到数组中的最高数字(使用apply
on Math.max
),然后找到该最高数字与 100 之间的比率。
After that, it's just a matter of looping through your array and dividing all your numbers by that ratio:
之后,只需循环遍历数组并将所有数字除以该比率即可:
var numbers = [3, 8, 45, 74, 123],
ratio = Math.max.apply(Math, numbers) / 100,
l = numbers.length,
i;
for (i = 0; i < l; i++) {
numbers[i] = Math.round(numbers[i] / ratio);
}
Here's the fiddle: http://jsfiddle.net/XpRR8/
这是小提琴:http: //jsfiddle.net/XpRR8/
Note:I'm using Math.round
to round the numbers to the nearest integer. If you instead prefer to keep them as floats, just remove the function call:
注意:我正在使用Math.round
将数字四舍五入到最接近的整数。如果您更喜欢将它们保留为浮点数,只需删除函数调用:
for ( i = 0; i < l; i++ ) {
numbers[i] /= ratio;
}
Here's the fiddle: http://jsfiddle.net/XpRR8/1/
这是小提琴:http: //jsfiddle.net/XpRR8/1/
If you don't have to support IE8 and below, you can use Array.prototype.map()
:
如果您不必支持 IE8 及以下,则可以使用Array.prototype.map()
:
var numbers = [3, 8, 45, 74, 123],
ratio = Math.max.apply(Math, numbers) / 100;
numbers = numbers.map(function (v) {
return Math.round(v / ratio);
});
Here's the fiddle: http://jsfiddle.net/XpRR8/2/
这是小提琴:http: //jsfiddle.net/XpRR8/2/
If you dosupport IE8, but are anyhow using jQuery, you can use $.map()
instead:
如果您确实支持 IE8,但无论如何都在使用 jQuery,则可以$.map()
改用:
numbers = $.map(numbers, function (v) {
return Math.round(v / ratio);
});
Here's the fiddle: http://jsfiddle.net/XpRR8/3/
这是小提琴:http: //jsfiddle.net/XpRR8/3/
Update:As pointed out by @wvxvw in the comments below, if you're concerned about fringe implementations that impose an artificial limit on the amount of arguments apply
will handle, then use a loop instead of Math.max.apply
. Here's an example (assuming neither Array.prototype.map
nor $.map
are available):
更新:正如@wvxvw 在下面的评论中指出的那样,如果您担心边缘实现会人为地限制apply
将处理的参数数量,那么请使用循环而不是Math.max.apply
. 这是一个示例(假设既不可用Array.prototype.map
也不$.map
可用):
var numbers = [3, 8, 45, 74, 123],
ratio = 0,
i = numbers.length;
while (i--) numbers[i] > ratio && (ratio = numbers[i]);
ratio /= 100;
i = numbers.length;
while (i--) numbers[i] = Math.round(numbers[i] / ratio);
Here's the fiddle: http://jsfiddle.net/XpRR8/4/
这是小提琴:http: //jsfiddle.net/XpRR8/4/
If you're using ES6, this becomes laughably simple:
如果您使用的是 ES6,这将变得非常简单:
var numbers = [3, 8, 45, 74, 123];
var ratio = Math.max(...numbers) / 100;
numbers = numbers.map(v => Math.round(v / ratio));
回答by Travis J
Like this
像这样
function Normalize(array, value)
{
for( var i = 0, len = array.length; i < len; i++ )
{
if( parseInt(array[i]) > value) array[i] = value;
}
}
And then use it:
然后使用它:
var arr = [];
arr.push(101);
arr.push(5);
arr.push(6);
Normalize(arr,100);
回答by Louis Ricci
function normalize(arr, max) {
// find the max value
var m = 0;
for(var x=0; x<arr.length; x++) m = Math.max(m, arr[x]);
// find the ratio
var r = max / m;
// normalize the array
for(var x=0; x<arr.length; x++) arr[x] = arr[x] * r;
return arr;
}
回答by Roozbeh Zabihollahi
Just an edit to LastCoder code to support Negative numbers as well
只需对 LastCoder 代码进行编辑即可支持负数
function normalize(arr, max) {
// find the max value
var max = arr[0];
var min = arr[0];
for(var x=0; x<arr.length; x++)
max = Math.max(m, arr[x];
for(var x=0; x<arr.length; x++)
min = Math.min(m, arr[x];
// normalize the array
for(var x=0; x<arr.length; x++)
arr[x] = (arr[x] - min) / (max - min);
return arr;
}
回答by Michael Krelin - hacker
You need to find the maximum and scale all numbers to the target range.
您需要找到最大值并将所有数字缩放到目标范围。
回答by Niet the Dark Absol
Well, you can get the maximum value with Math.max.apply(arr)
, then either loop through or use arr.map
to multiply all numbers by 100/max
. Done.
好吧,您可以使用 获得最大值Math.max.apply(arr)
,然后循环遍历或使用arr.map
将所有数字乘以100/max
。完毕。
回答by Cybernetic
Pass your array into the following function:
将您的数组传递给以下函数:
function normalize_array(arr) {
normalize = function(val, max, min) {
return(val - min) / (max - min);
}
max = Math.max.apply(null, arr)
min = Math.min.apply(null, arr)
hold_normed_values=[]
arr.forEach(function(this_num) {
hold_normed_values.push(normalize(this_num, max, min))
})
return(hold_normed_values)
}
Example:
示例:
nums = [10, 20, 30, 40, 50]
Usage:
用法:
normalize_array(mums)