
[英]Using Math.max and Math.min to remove highest and lowest number from array
[英]Math.max not filtering an array by highest number
我正在尝试使用以下代码返回一个包含每个嵌套数组的最大数量的数组:
const triangles = [
[6, 7, 10],
[9, 3, 6],
[6, 3, 5],
[6, 13, 12],
[7, 12, 8],
];
function test(triangles){
return triangles.filter(triangle => Math.max(...triangle));
}
console.log(test(triangles));
它没有过滤任何东西,只是返回相同的值,但我不明白为什么。
您需要 map 外部(三角形)数组中的每个(边)数组,到最大边。
const triangles = [ [6, 7, 10], [9, 3, 6], [6, 3, 5], [6, 13, 12], [7, 12, 8], ]; const test = (triangles) => triangles.map(sides => Math.max(...sides)); console.log(test(triangles)); // [10, 9, 6, 13, 12]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.