简体   繁体   English

返回数组中的最大数(数组包含一个字符串)?

[英]Return the biggest number in the array (array contains a string)?

I dont understand why my code is not working, when I read it logically I feel that it should work, but what it does is return 3,4,2 as opposed to the highest number of the 3 (ie 4)我不明白为什么我的代码不起作用,当我从逻辑上阅读它时,我觉得它应该起作用,但它所做的是返回 3,4,2 而不是 3 中的最高数字(即 4)

const array2 = ['a', 3, 4, 2] // should return 4

for(items of array2){
    if(items > 0) {
        console.log(Math.max(items));
}

What am I doing wrong?我究竟做错了什么? What have I misinterpreted?我误解了什么? Please don't give me the answer, just tell me why my logic does'nt work请不要给我答案,只是告诉我为什么我的逻辑不起作用

in for-loop, items is just one item actually.在 for 循环中, items实际上只是一项。 Each time, you print the current item.每次,您打印当前项目。 it is basically the same thing with this这基本上是一样的

const array2 = ['a', 3, 4, 2] // should return 4

for(items of array2){
    if (items > 0) {
        console.log(items);
    }
}

you can do it with this only你只能这样做

const array2 = ['a', 3, 4, 2] // should return 4

console.log(Math.max(...array2.map(s => +s || Number.MIN_SAFE_INTEGER)));

check out: https://stackoverflow.com/a/44208485/16806649签出: https://stackoverflow.com/a/44208485/16806649

if it was integer-only array, this would be enough:如果它是纯整数数组,这就足够了:

const array2 = [3, 4, 2] // should return 4

console.log(Math.max(...array2));

I don't think you need the "For(items of arr)" instead just if the length of the array is greater than 0, then console.log(Math.max(...arr) should work.我认为您不需要“For(arr 的项目)”,而是仅当数组的长度大于 0 时,console.log(Math.max(...arr) 应该可以工作。

See document ion below:请参阅下面的文档离子:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

  • You just need to filter the array.您只需要过滤数组。
  • Below example I am using filter() method of array and then just pass that filteredarray to Math.max() function.下面的示例我使用数组的filter()方法,然后将该filteredarray数组传递给Math.max() function。
  • isNan() function returns false for valid number. isNan() function 对有效数字返回 false。
  • Math.max(...filteredArr) is using spred operator to pass the values. Math.max(...filteredArr)正在使用spred运算符来传递值。

 const arr = ['a', 3, 4, 2]; const filteredArr = arr.filter(val => { if (;isNaN(val)) return val. }) console.log(Math.max(..;filteredArr));

It is returning 3,4,2 because you are taking array2, and iterating through with each individual element of the array.它返回 3,4,2,因为您正在使用 array2,并遍历数组的每个单独元素。 items is not the entire array, it is the individual element and that is why Math.max of an individual element is just getting the same value. items 不是整个数组,它是单个元素,这就是单个元素的 Math.max 只是获得相同值的原因。

just you need fliter you're array then get max value, also in arrayFilters function use to removes everything only return numbers of this array只是你需要过滤你的数组然后获得最大值,也在arrayFilters function 用于删除所有内容只返回这个数组的数字

 function arrayFilters(array){ const number = array.filter(element => typeof element === 'number') return number; } function getMaxValue(number){ return Math.max.apply(null,number) } const arr = ['a',2,3,4]; console.log(getMaxValue(arrayFilters(arr)))

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

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