1
data = [
    {
        "group": "banana",
        "Nitrogen": "12",
        "normal": "15",
        "stress": "1"
    },
    {
        "group": "poacee",
        "Nitrogen": "6",
        "normal": "6",
        "stress": "20"
    },
    {
        "group": "sorgho",
        "Nitrogen": "11",
        "normal": "28",
        "stress": "12"
    },
    {
        "group": "triticum",
        "Nitrogen": "19",
        "normal": "20",
        "stress": "12"
    }
];

I have this array to use in a grouped bar chart. I want to get the maximum value out of all the values, which is 28 in this.

Tobias S.
  • 21,159
  • 4
  • 27
  • 45
  • do you just need the highest value or the value with it's key? – AdityaParab Jul 11 '22 at 11:24
  • Examples of this in here - https://stackoverflow.com/questions/4020796/finding-the-max-value-of-an-attribute-in-an-array-of-objects – 2189490 Jul 11 '22 at 11:25
  • Do you wish to get the object with the maximum `normal` value or do you wish to get only the max `normal` value? – ruth Jul 11 '22 at 11:46

4 Answers4

1

As an alternative way to the other answers, you could sort your array from the highest to the lowest based on the normal property and select the first element:

data.sort((a, b) => parseInt(b.normal, 10) - parseInt(a.normal, 10));
console.log(data[0]); // The element with the highest normal value
console.log(data[0].normal) // Or just the highest normal value
ilpianoforte
  • 1,180
  • 1
  • 10
  • 28
0

I asume you want the max value from the normal property.

const data = [
    {
        "group": "banana",
        "Nitrogen": "12",
        "normal": "15",
        "stress": "1"
    },
    {
        "group": "poacee",
        "Nitrogen": "6",
        "normal": "6",
        "stress": "20"
    },
    {
        "group": "sorgho",
        "Nitrogen": "11",
        "normal": "28",
        "stress": "12"
    },
    {
        "group": "triticum",
        "Nitrogen": "19",
        "normal": "20",
        "stress": "12"
    }
  ];
  
  const max = data.map(x => +x.normal).reduce((a, b) => a > b ? a : b, 0);
  
  console.log(`Max: ${max}`);
Norbert Bartko
  • 2,468
  • 1
  • 17
  • 36
  • 1
    This definitely works for the `normal` property, but I'd rather ask for clarification from the OP first. (and in case it's only the `normal` property, the question is probably a dupe) –  Jul 11 '22 at 11:35
0

Just extract all the numeric values from all the objects in the array, concat them to a collective array, and then find out the max.

const data = [
  {
    "group": "banana",
    "Nitrogen": "12",
    "normal": "15",
    "stress": "1"
  },
  {
    "group": "poacee",
    "Nitrogen": "6",
    "normal": "6",
    "stress": "20"
  },
  {
    "group": "sorgho",
    "Nitrogen": "11",
    "normal": "28",
    "stress": "12"
  },
  {
    "group": "triticum",
    "Nitrogen": "19",
    "normal": "20",
    "stress": "12"
  }
];

const getMax = (arr) => {
  const op = arr.reduce((acc, cur) => {
    const vals = Object.values(cur).map(v => +v).filter(v => v);
    return acc.concat(vals);
  }, []);

  return Math.max.apply(null, op);
}

console.log(getMax(data)); // 28
AdityaParab
  • 7,024
  • 3
  • 27
  • 40
-1

Well below code works

var data= [
    {
        "group": "banana",
        "Nitrogen": "12",
        "normal": "15",
        "stress": "1"
    },
    {
        "group": "poacee",
        "Nitrogen": "6",
        "normal": "6",
        "stress": "20"
    },
    {
        "group": "sorgho",
        "Nitrogen": "11",
        "normal": "28",
        "stress": "12"
    },
    {
        "group": "triticum",
        "Nitrogen": "19",
        "normal": "20",
        "stress": "12"
    }
  ];
let max = 0;
let finalObject;
for(let  i = 0 ; i < data.length; i++){
   var newMax = Math.max(parseInt(data[i].Nitrogen), parseInt(data[i].normal), parseInt(data[i].stress));
   if(newMax > max){
       max = newMax;
       finalObject = data[i];
   }
   
}
console.log(finalObject);
nobalG
  • 4,544
  • 3
  • 34
  • 72