简体   繁体   English

使用函数和参数从关联数组中获取最大值或最小值

[英]Get max or min value from associative array with function and parameters

Here is the code as far as I got it right now: 这是我目前所获得的代码:

<!DOCTYPE html>
<html>
<body>

<button onclick="scanarray('a', 'max')">Test with a, max</button>

<button onclick="scanarray('b', 'min')">Test with b, min</button>



<p id="demo">test</p>

<script>



var array = [{"a":1},{"b":3},{"a":6},{"b":10}];


var max = null;
var min = null;
var value = null;

function scanarray(scanval, searchterm) {

  if (array.length < 1) {
    return -1;
  }

    if(searchterm == "max"){

    max = Math.max.apply(Math,array.map(function(e){return e.scanvalue;}))

    }else if (searchterm == "min"){

    min = Math.min.apply(Math,array.map(function(e){return e.scanval;}))

}else
{document.getElementById("demo").innerHTML = "Only max and min available";}

    if(searchterm == "max"){

        document.getElementById("demo").innerHTML = "Search: " + scanval +"  "+ "Value: " + max;
    }

    if(searchterm == "min"){
        document.getElementById("demo").innerHTML = "Search: " + scanval +"   "+ "Value: " + min;
    }

}
</script>

</body>
</html>

The above should give me a and 6 or b and 3 as results. 以上应该给我a和6或b和3作为结果。 However I get NaN as result for the value part. 但是我得到NaN作为价值部分的结果。 When using "return ea" in the Math section and only having a as keys it works. 在“数学”部分中使用“ return ea”并且仅将a作为键时,它可以工作。

I want to be able to determin the max or min value of a key I enter as parameter to the function. 我希望能够确定作为函数参数输入的键的最大值或最小值。

Hope you can help me here. 希望你能在这里帮助我。

Thanks in advance. 提前致谢。

TheVagabond 流浪者

There are some naming mess in your code. 您的代码中有些命名混乱。 For example scanvalue is name of your function but you are trying to reach it as a parameter of e( e.scanvalue ). 例如, scanvalue是函数的名称,但是您尝试将其作为e( e.scanvalue )的参数来e.scanvalue I should be scanval . 我应该是scanval But still there are some problems. 但是仍然存在一些问题。 You can't reach property "a" or "b" of e if e.scanval . 如果e.scanval则无法访问e的属性“ a”或“ b”。 You're trying to reach variable of variable . 您正在尝试达到variable的变量

Then, you should use e[scanval] . 然后,您应该使用e[scanval] It returns you to value of "a" or "b". 它使您返回到“ a”或“ b”的值。 But if object doesn't have one of them? 但是,如果对象没有其中之一? Then you should add "|| 0" to get correct value. 然后,您应该添加“ || 0”以获得正确的值。 (Instead of NaN or undefined) It means that; (而不是NaN或未定义) 是指; use e[scanval] if its valid, if not use 0. 如果有效,请使用e [scanval],否则请不要使用0。

Use this; 用这个;

return e[scanval] || 0;

If your boundaries include some negative values, use something like -9999 or -Infinity. 如果边界包括一些负值,请使用-9999或-Infinity之类的值。

You are getting Nan because e.scanvalue returns undefined in your case. 您得到Nan的原因是e.scanvalue在您的情况下返回undefined。

Try using custom function. 尝试使用自定义功能。

function getValue(arr){
var res = [];
for(var j = 0; j < arr.length; j++)
{

    for(var anItem in arr[j])
    {
        res.push(arr[j][anItem]);
    }
}
return res;
}

and call this function like 并像这样调用此函数

max = Math.max.apply(Math,getValue(array))

and

min = Math.min.apply(Math,getValue(array))

accordingly. 相应地。 Hope this helps!!!! 希望这可以帮助!!!!

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

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