Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.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
Javascript 如何从5个变量中找到更大的值并返回变量名?_Javascript_Node.js - Fatal编程技术网

Javascript 如何从5个变量中找到更大的值并返回变量名?

Javascript 如何从5个变量中找到更大的值并返回变量名?,javascript,node.js,Javascript,Node.js,我有5个变量,随机分配了一个0-500之间的数字,我想知道如何比较所有5个变量,然后在控制台中返回最大值的变量名 变量名是:count1、count2、count3、count4、count5 谢谢大家! 您可以这样做: var x = [ { val: count1, variableName: "count1" }, { val: count2, variableName: "count2" }, { val: count3, variableName: "count

我有5个变量,随机分配了一个0-500之间的数字,我想知道如何比较所有5个变量,然后在控制台中返回最大值的变量名

变量名是:count1、count2、count3、count4、count5

谢谢大家!

您可以这样做:

var x = [
    { val: count1, variableName: "count1" },
    { val: count2, variableName: "count2" },
    { val: count3, variableName: "count3" },
    { val: count4, variableName: "count4" },
    { val: count5, variableName: "count5" }];
在属性val上按升序对数组x排序


x[4]。variableName是最大值持有者的名称。

按照以下步骤操作

  • 创建一个对象数组。其中,每个对象都是变量名与其值之间的映射
  • 根据value属性按升序对数组排序
  • 取第四个元素的variableName属性
  • 下面是代码片段

    // assuming these are the random numbers from 0 - 500
    let count1=10, count2=30, count3=15, count4=7, count5=23;
    
    // create an array of objects. Where each object is a mapping between the variable name and its value
    let tmpArray=[
      {
        value: count1,
        variableName: 'count1'
      },
      {
        value: count2,
        variableName: 'count2'
      },
      {
        value: count3,
        variableName: 'count3'
      },
      {
        value: count4,
        variableName: 'count4'
      },
      {
        value: count5,
        variableName: 'count5'
      }
    ]
    
    // sort the array in ascending order based on the value property and then take the 4th element's variableName property
    let highesVariableName=tmpArray.sort((a,b)=>a.value-b.value)[4].variableName;
    
    console.log(highesVariableName); // count2
    
    var countobj={count1:230,count2:123,count3:428,count4:67,count5:198};
    var arrVals=Object.keys(countobj.map)(函数(key){return countobj[key];});
    var minVal=Math.min.apply(null,arrVals);
    var maxVal=Math.max.apply(null,arrVals);
    var minVar=Object.keys(countobj).filter(函数(键){return countobj[key]==minVal})[0];
    var maxVar=Object.keys(countobj).filter(函数(键){return countobj[key]==maxVal})[0];
    
    write('最小值为'+minVal+'来自变量'+minVar+',最大值为'+maxVal+'来自变量'+maxVar')通过创建对象很容易找到最大值:-

    var count1 = 100, count2 = 200, count3 = 300, count4 = 400, count5 = 500;
    
    
    var tempObj = {
     "count1" : count1,
     "count2" : count2,
     "count3" : count3,
     "count4" : count4,
     "count5" : count5
    }
    
    var max  = Object.entries(tempObj).reduce((prev, current) => (prev[1] > current[1]) ? prev : current)[0];
    

    到目前为止,您尝试了什么?这只返回最大值。不是具有最大值的变量的名称。哦,您需要变量的名称。我读错了,让我修改代码。然后你必须维护值和变量名之间的映射才能实现这一点是的,我刚刚修改了上面的代码。排序非常简单,所以我没有添加排序代码。