1
var a = 1;
var b = 2;
var c = 3;

var d = Math.max(a, b, c);
return d;

This code is returning 3. I need a result with not only the computed value, but with a variable name also.

Desired output: c - 3.

Matt Morgan
  • 4,900
  • 4
  • 21
  • 30
Presto
  • 31
  • 2

5 Answers5

3

Unfortunately, the names of variables are not easily retrievable in JavaScript.

Such a solution is not scalable either.

However, an adjustment to the data structures that you leverage can solve this.

Note the use of Object, Object.entries() and Array.prototype.reduce() in the example below.

// Pairs.
const pairs = {
  a: 1,
  b: 2,
  c: 3
}

// Get Max Pair.
const getmaxpair = (pairs) => Object.entries(pairs).reduce((max, pair) => !max && pair ||  pair[1] > max[1] ? pair : max, false)

// Max Pair.
const [key, value] = getmaxpair(pairs)

// Log.
console.log('max pair:', `${key} ${value}`) // c 3
Arman Charan
  • 5,669
  • 2
  • 22
  • 32
2

If you are okay with storing those values in an object, you can loop through the keys and compare their values while storing the name of the key.

var values = { a: 1, b: 2, c: 3 }

var maxName;
var max;
for(var key in values) {
  if(max === undefined || max < values[key]){
    maxName = key;
    max = values[key];
  }
}

console.log('Max: ' + maxName + ' - ' + values[maxName]);

    var values = { a: 1, b: 2, c: 3 }

    var maxName;
    var max;
    for(var key in values) {
      if(max === undefined || max < values[key]){
        maxName = key;
        max = values[key];
      }
    }
    
    console.log('Max: ' + maxName + ' - ' + values[maxName]);
IrkenInvader
  • 4,030
  • 1
  • 12
  • 23
1

You can do this easily with javascript objects or maps

var list = {"a": 1, "b": 2, "c": 3};

var d = Math.max(list.a, list.b, list.c);

// this will return an array: ["a", "b", "c"]
keys = Object.keys(list);

// do a reverse lookup here to find which key your value belongs to
// eg. if list[keys[counter]] == d { your code here }

Here is another way of doing reverse lookups https://stackoverflow.com/a/9907509/9310329 Hope this helps

0

Although this could be more easily achieved using a map (see other answers) in a realistic use case. What you're asking for is actually also possible (inspired from https://stackoverflow.com/a/42791996/7988438 )

let a = 1;
let b = 2;
let c = 3;

let d = Math.max(a, b, c);


let varToString = varObj => Object.keys(varObj)[0];

if (a == d) {
    console.log(varToString({a}),d)
}
if (b == d) {
    console.log(varToString({b}),d)
}
if (c == d) {
    console.log(varToString({c}),d)
}

Quite ugly, but gets the job done.

0

While the magic you think about is possible to some extent with iterating over object keys as shown in the other answers, often you just want an array of objects instead, where the keys are known, and the values vary:

var persons=[
  {name:  "John", age: 52},
  {name:  "Jane", age: 35},
  {name: "Peter", age: 86},
  {name: "Susan", age: 72}
];

var eldest=persons[0];
persons.forEach(function(person){
  if(person.age>eldest.age)eldest=person;
});
console.log(eldest);
tevemadar
  • 12,389
  • 3
  • 21
  • 49