Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScriptReduce过滤对象数组,以找到最低的计算统计数据_Javascript_Arrays - Fatal编程技术网

使用JavaScriptReduce过滤对象数组,以找到最低的计算统计数据

使用JavaScriptReduce过滤对象数组,以找到最低的计算统计数据,javascript,arrays,Javascript,Arrays,所以,我写了这个循环 if (specimins.length > 0) { var max = specimins[0] for (var x = 1; x < specimins.length; x++) { if ((max.weight+max.height) > (specimins[x].weight+specimins[x].height)) { max = specimins[x] }

所以,我写了这个循环

if (specimins.length > 0) {
    var max = specimins[0]
    for (var x = 1; x < specimins.length; x++) {
        if ((max.weight+max.height) > (specimins[x].weight+specimins[x].height)) {
            max = specimins[x]
        }
    }
    specimins = [max]
}
if(specimins.length>0){
var max=规格[0]
对于(变量x=1;x(规格[x]。重量+规格[x]。高度)){
最大值=规格[x]
}
}
specimins=[最大值]
}
然而,这是一个相当碍眼的问题。因此,基本上,它是通过一系列的样本来找到计算出的最低总重量+高度。有没有一种好方法可以让这个数组使用。reduce()?我尝试过一些尝试,但大多数都不适合我


感谢您的帮助。谢谢大家!

您可以使用
reduce
获得高度+宽度最低的元素,如下所示:

var max = specimens.reduce(function(a, b) {
  return (a.width + a.height) < (b.width + b.height) ? a : b
}, specimens[0])
var max=samples.reduce(函数a,b){
返回(a.宽度+a.高度)<(b.宽度+b.高度)?a:b
},样本[0])

但是,在这种情况下,使用循环的标准没有错。

我个人认为,记住语法糖并不总是更好的选择,因为它的代码行更少,所以这一点并不重要。您可能会牺牲可读性来实现brevity@Jacob谢谢你的编辑,虽然我对调用最低的变量有点怀疑(是的,我只是觉得这个变量的名字应该和OP中的名字一样。非常感谢,它成功了!麦克斯是其他事情留下来的,谢谢你也指出了这一点。@Damon现在就要改变这一点。干杯@Neal没问题,如果您选择我的答案为“接受答案”,我将不胜感激