Home > database >  How to get a max value from a array of Objects
How to get a max value from a array of Objects

Time:07-11

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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