Javascript 如何在javascript中找到嵌套数组的最大值/最小值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10564441/
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 find the max/min of a nested array in javascript?
提问by nachocab
I want to find the maximum of a nested array, something like this:
我想找到嵌套数组的最大值,如下所示:
a = [[1,2],[20,3]]
d3.max(d3.max(a)) // 20
but my array contains a text field that I want to discard:
但我的数组包含一个我想丢弃的文本字段:
a = [["yz",1,2],["xy",20,3]]
d3.max(a) // 20
回答by mbostock
If you have a nested array of numbers (arrays = [[1, 2], [20, 3]]
), nest d3.max:
如果您有一个嵌套的数字数组 ( arrays = [[1, 2], [20, 3]]
),请嵌套d3.max:
var max = d3.max(arrays, function(array) {
return d3.max(array);
});
Or equivalently, use array.map:
或者等效地,使用array.map:
var max = d3.max(arrays.map(function(array) {
return d3.max(array);
}));
If you want to ignore string values, you can use array.filterto ignore strings:
如果要忽略字符串值,可以使用array.filter忽略字符串:
var max = d3.max(arrays, function(array) {
return d3.max(array.filter(function(value) {
return typeof value === "number";
}));
});
Alternatively, if you know the string is always in the first position, you could use array.slicewhich is a bit more efficient:
或者,如果您知道字符串总是在第一个位置,您可以使用更高效的array.slice:
var max = d3.max(arrays, function(array) {
return d3.max(array.slice(1));
});
Yet another option is to use an accessor function which returns NaN
for values that are not numbers. This will cause d3.max to ignore those values. Conveniently, JavaScript's built-in Number
function does exactly this, so you can say:
另一种选择是使用访问器函数,该函数返回NaN
非数字的值。这将导致 d3.max 忽略这些值。方便的是,JavaScript 的内置Number
函数正是这样做的,所以你可以说:
var max = d3.max(arrays, function(array) {
return d3.max(array, Number);
});
回答by kbec
Use this:
用这个:
function arrmax(arrs) {
var toplevel = [];
var f = function(v) {
return !isNaN(v);
};
for (var i = 0, l = arrs.length; i<l; i++) {
toplevel.push(Math.max.apply(window, arrs[i].filter(f)));
}
return Math.max.apply(window, toplevel);
}
or better:
或更好:
function arrmax(arrs) {
if (!arrs || !arrs.length) return undefined;
var max = Math.max.apply(window, arrs[0]), m,
f = function(v){ return !isNaN(v); };
for (var i = 1, l = arrs.length; i<l; i++) {
if ((m = Math.max.apply(window, arrs[i].filter(f)))>max) max=m;
}
return max;
}
See MDNfor Array.filter method details.
有关Array.filter 方法的详细信息,请参阅MDN。
回答by kennebec
You can flatten an array and apply a function to each member
您可以展平数组并将函数应用于每个成员
Array.prototype.flatten= function(fun){
if(typeof fun!= 'function') fun= '';
var A= [], L= this.length, itm;
for(var i= 0; i<L; i++){
itm= this[i];
if(itm!= undefined){
if(!itm.flatten){
if(fun) itm= fun(itm);
if(itm) A.push(itm);
}
else A= A.concat(itm.flatten(fun));
}
}
return A;
}
var a= [["yz", 1, 2], ["xy", 20, 3]], max=-Infinity;
var max=Math.max.apply(a, a.flatten(Number));
回答by Rick
If you now exactly what columns you want to test, you can use:
如果您现在确切要测试哪些列,则可以使用:
var columns = ["ColumnA", "ColumnB", "ColumnC"];
var max = selectedMax(columns,dataset);
var min = selectedMin(columns,dataset)
function selectedMax(columns, dataset) {
var max;
columns.forEach(function(element, index, array) {
var tmpmax = d3.max(dataset, function(d) {
return +d[element];
});
max = (tmpmax > max || max === undefined) ? tmpmax : max;
});
return max;
}
function selectedMin(columns, dataset) {
var min;
columns.forEach(function(element, index, array) {
var tmpmin = d3.min(dataset, function(d) {
return +d[element];
});
min = (tmpmin < min || min === undefined) ? tmpmin : min;
});
return min;
}
回答by Xavier Guihot
d3.array
provides d3.merge
which flattens an array of arrays.
Coupled with d3.max
and javascript's Number
as an accessor:
加上d3.max
和 javascriptNumber
作为访问器:
var max = d3.max(d3.merge(arrays), Number);
For example:
例如:
var input = [["yz", 1, 2], ["xy", 20, 3]];
var max = d3.max(d3.merge(input), Number);
console.log(max);
<script src="https://d3js.org/d3-array.v2.min.js"></script>
回答by btown
It's a cruel hack, but looking at the source codefor d3.max
, your best bet might be to define a d3.max1
that discards the first element by copying that code, but replacing i=-1
with i=0
. The code at that link is excerpted here. Note that I'm not a regular d3.js user, but from what I know of the library, you're going to want make sure your version has an f.call
case like this function does, so that it can respond to live updates correctly.
这是一个残酷的黑客,而是看源代码的d3.max
,你最好的选择可能是定义d3.max1
该丢弃通过复制代码,但更换的第一个元素i=-1
用i=0
。该链接上的代码摘录在此处。请注意,我不是一个普通的 d3.js 用户,但根据我对这个库的了解,你会想要确保你的版本有一个f.call
像这个函数那样的情况,以便它可以正确响应实时更新。
d3.max = function(array, f) {
var i = -1,
n = array.length,
a,
b;
if (arguments.length === 1) {
while (++i < n && ((a = array[i]) == null || a != a)) a = undefined;
while (++i < n) if ((b = array[i]) != null && b > a) a = b;
} else {
while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined;
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
}
return a;
};
Then it would just be d3.max(d3.max1(a))
.
那么就可以了d3.max(d3.max1(a))
。