简体   繁体   English

Javascript:找不到嵌套在对象中的数组的最大值

[英]Javascript: Can't find max value for array nested in object

I am expecting this code to return 4 as the maximum value of the array on obj.key , however the code below but returns 1 . 我期望此代码返回4作为obj.key数组的obj.key ,但是下面的代码却返回1

Can someone help fix this, and achieve this in a shorter way? 有人可以帮助解决此问题,并以较短的方式实现吗?

 var obj = { key: [1, 2, 4] } function getLargestElementAtProperty(obj, key) { if (!Array.isArray(obj[key])) { return undefined } for (num in obj[key]) { return Math.max(obj[key][num]) } } var output = getLargestElementAtProperty(obj, 'key'); console.log(output); // --> expected 4 

The issue is that a function can only return once. 问题是一个函数只能返回一次。 So when you first iterate over the array and get one then it returns and the function is done running. 因此,当您第一次遍历数组并获得一个数组时,它将返回并且函数已完成运行。 Math.max() returns the max from 0 or more numbers, so if you only pass it one number it will always return that same number. Math.max()返回0或多个数字中的最大值,因此,如果仅传递一个数字,它将始终返回该数字。 Something like below will work. 像下面这样的东西会起作用。

 const obj = { key: [1, 2, 4] } function getLargestElementAtProperty(obj, key) { let largest; if (!Array.isArray(obj[key])) { return undefined } for (num in obj[key]) { if (!largest || largest < obj[key][num]) { largest = obj[key][num]; } } return largest; } var output = getLargestElementAtProperty(obj, 'key'); console.log(output); // --> expected 4 

Also if you can use Math.max and the spread operator to achieve the same thing in a more modern JavaScript way like so: 另外,如果您可以使用Math.max和spread运算符以更现代的JavaScript方式实现相同的目的,例如:

 const obj = { key: [1, 2, 4] } function getLargestElementAtProperty(obj, key) { if (!Array.isArray(obj[key])) { return undefined } return Math.max(...obj[key]); } var output = getLargestElementAtProperty(obj, 'key'); console.log(output); 

Hope that helps! 希望有帮助!

Your function returns 1 because the for .. in loop immediately returns the first item iterated in the array obj[key] . 您的函数返回1因为for .. in循环立即返回数组obj[key]迭代的第一项。

Consider revising your code by using the Array#reduce method as detailed to below, to find and return the maximum value of the input array: 考虑使用如下详述的Array#reduce方法修改代码,以查找并返回输入数组的最大值:

 var obj = { key: [1, 2, 4] } function getLargestElementAtProperty(obj, key) { if (!Array.isArray(obj[key])) { return undefined } var array = obj[key]; /* Use reduce to iterate through each item in the array, comparing it with the previously found max value. Note that the first iteration will find the max of the Math.max(1,1), which will 1 */ var max = array.reduce(function(currentMax, currentItem) { return Math.max(currentMax, currentItem) }) return max; } var output = getLargestElementAtProperty(obj, 'key'); console.log(output); // --> expected 4 

So I found the solution which was removing the for loop and using .apply with Math.max and my code was accepted, thanks for the help guys So this is my new code ! 因此,我找到了删除for循环并在Math.max中使用.apply的解决方案,并接受了我的代码,感谢帮助人员,这是我的新代码!

 var obj = { key: [1, 2, 4] } function getLargestElementAtProperty(obj, key) { if (!Array.isArray(obj[key]) || obj[key].length === 0) { return undefined } else{ return Math.max.apply(null, obj[key]) } } var output = getLargestElementAtProperty(obj, 'key'); console.log(output); // --> expected 4 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM