Carlos Franco
Carlos Franco

Reputation: 58

Javascript: Can't find max value for array nested in object

I am expecting this code to return 4 as the maximum value of the array on obj.key, however the code below but returns 1.

Can someone help fix this, and achieve this in a shorter way?

var obj = {
  key: [1, 2, 4]
}
function getLargestElementAtProperty(obj, key) {
   if (!Array.isArray(obj[key])) {
      return undefined
   }
   for (num in obj[key]) {
      return Math.max(obj[key][num])
   }
}

var output = getLargestElementAtProperty(obj, 'key');
console.log(output); // --> expected 4

Upvotes: 0

Views: 263

Answers (3)

Carlos Franco
Carlos Franco

Reputation: 58

So I found the solution which was removing the for loop and using .apply with Math.max and my code was accepted, thanks for the help guys So this is my new code !

var obj = {
  key: [1, 2, 4]
}
function getLargestElementAtProperty(obj, key) {
   if (!Array.isArray(obj[key]) || obj[key].length === 0) {
      return undefined
   } else{
      return Math.max.apply(null, obj[key])
   }
}

var output = getLargestElementAtProperty(obj, 'key');
console.log(output); // --> expected 4

Upvotes: 0

CascadiaJS
CascadiaJS

Reputation: 2495

The issue is that a function can only return once. So when you first iterate over the array and get one then it returns and the function is done running. Math.max() returns the max from 0 or more numbers, so if you only pass it one number it will always return that same number. Something like below will work.

const obj = {
  key: [1, 2, 4]
}
function getLargestElementAtProperty(obj, key) {
  let largest;
   if (!Array.isArray(obj[key])) {
      return undefined
   }
   for (num in obj[key]) {
      if (!largest || largest < obj[key][num]) {
        largest = obj[key][num];
      }
   }
   return largest;
}

var output = getLargestElementAtProperty(obj, 'key');
console.log(output); // --> expected 4

Also if you can use Math.max and the spread operator to achieve the same thing in a more modern JavaScript way like so:

const obj = {
  key: [1, 2, 4]
}
function getLargestElementAtProperty(obj, key) {
   if (!Array.isArray(obj[key])) {
      return undefined
   }
   return Math.max(...obj[key]);
}

var output = getLargestElementAtProperty(obj, 'key');
console.log(output);

Hope that helps!

Upvotes: 2

Dacre Denny
Dacre Denny

Reputation: 30370

Your function returns 1 because the for .. in loop immediately returns the first item iterated in the array obj[key].

Consider revising your code by using the Array#reduce method as detailed to below, to find and return the maximum value of the input array:

var obj = {
  key: [1, 2, 4]
}
function getLargestElementAtProperty(obj, key) {
   if (!Array.isArray(obj[key])) {
      return undefined
   }
   
   var array = obj[key];
   
   /*
   Use reduce to iterate through each item in the array, comparing
   it with the previously found max value. Note that the first 
   iteration will find the max of the Math.max(1,1), which will 1
   */
   var max = array.reduce(function(currentMax, currentItem) {
      return Math.max(currentMax, currentItem)
   })
   
   return max;
}

var output = getLargestElementAtProperty(obj, 'key');
console.log(output); // --> expected 4

Upvotes: 1

Related Questions