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.

3
  • do you just need the highest value or the value with it's key? Commented Jul 11, 2022 at 11:24
  • Examples of this in here - stackoverflow.com/questions/4020796/… Commented Jul 11, 2022 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? Commented Jul 11, 2022 at 11:46

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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}`);

1 Comment

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)
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

Comments

-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);

4 Comments

This wouldn't compile just right, because Math.max(...) expects an array of number while you're passing an array of string.
sure, let me add parseInt @MikeS.
There has to be something better than an O(n²) solution.
@2189490 agreed, will be glad if anyone can share the answer. Looking forward towards it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.