Skip to content Skip to sidebar Skip to footer

Return The Highest Number From Object Properties Value

I have the following inside an ajax call: for (var key in json.rating) { var numb = Math.max(json.rating[key]); } console.log(numb); 'rating':{

Solution 1:

Problem:

The main problem here is that you are simply iterating object keys and rewriting the key value inside your numb variable, because you are passing only one argument to Math.max() which is the iterated number.

So you will always get the last iterated value, that explains why you got 92as result.

Answer :

In fact you should initialize the numb vraiable and always compare it with every iterated value, this is an edited snippet:

var json = {
  "rating": {
    "overall": 92,
    "atmosphere": 93,
    "cleanliness": 94,
    "facilities": 89,
    "staff": 94,
    "security": 92,
    "location": 88,
    "valueForMoney": 92
  }
};

var numb = 0;
for (var key in json.rating) {
  numb = Math.max(json.rating[key], numb);
}
document.write(numb);

Note:

This assumes that all your values are positive numbers that's why I initialized it to 0, otherwise just initialize numb to -Infinity.

Solution 2:

You should start off by declaring numb as the minimum value that you could expect, then when you iterate through your loop, pass the existing value for testing against the current value.

var numb = -999999;
    for( var key in json.rating ) {
      numb = Math.max(numb, json.rating[key]);
    }
    console.log(numb);

Solution 3:

You can do it this way:

var rating = {
"overall": 92,
"atmosphere": 93,
"cleanliness": 94,
"facilities": 89,
"staff": 94,
"security": 92,
"location": 88,
"valueForMoney": 92
}

//get all the values in an arrayvar allvalues = Object.keys(rating).map(function(key){return rating[key];});

// get the max value from the arrayconsole.log(Math.max.apply(null, allvalues)); 

Solution 4:

You should calculate the max value. one way to do it is:

var maxNumber = -Infinity;
for (var key in json.rating) {
    maxNumber = Math.max(maxNumber, json.rating[key]);
}
console.log(maxNumber);

this way, you constantly comparing the current value with the saved max value

Solution 5:

The function Math.max() takes a sequence of numbers and returns the highest of that sequence.

In your case: You call max() many times with only a single argument every time, and each time max() returns only that argument. So whatever you passed in your last call is the final value of numb.

Solution 1: Build a list of numbers using your loop, and then call max() once and pass that list. However, this iterates twice over your sequence of numbers (Note the use of the spread operator.)

numbers = []
for (var key in json.rating) {
    numbers.push(json.rating[key]);
}
numb = Math.max(...numbers);
console.log(numb);

Solution 2: Hold the largest value and compare it against the value of the current iteration. This iterates only once over your sequence of numbers.

numb = Number.MIN_VALUE;
for (var key in json.rating) {
    numb = Math.max(numb, json.rating[key]);
}
console.log(numb);

Post a Comment for "Return The Highest Number From Object Properties Value"