Skip to content
Advertisement

How to find maxium value from nested array Object in Javascript?

how to find biggest value for particular array objects from nested array.

My Javascript Array:

const Application = [
  {
    chartData: [
      {
        title: "Type 1",
        value: 60,
      },
      {
        title: "Type 2",
        value: 21,
      },
      {
        title: "Type 3",
        value: 4,
      },
    ],
    name: "App 1",
  },
  {
    chartData: [
      {
        title: "Type 1",
        value: 34,
      },
      {
        title: "Type 2",
        value: 45,
      },
      {
        title: "Type 3",
        value: 8,
      },
    ],
    name: "App 2",
  },
  {
    chartData: [
      {
        title: "Type 1",
        value: 59,
      },
      {
        title: "Type 2",
        value: 1,
      },
      {
        title: "Type 3",
        value: 3,
      },
    ],
    name: "App 3",
  },
];

I want to find the maximum value of the nested chartData array.

I want a method to calculate the maximum value and for the above data the output should be 60.

Can anyone help ?

Advertisement

Answer

you can use this way:

const maxValue = Math.max.apply(
  Math,
  ...Application.map((e) => {
    return e.chartData.map((el) => {
      return el.value
    })
  })
) //[60,21,4,34,45,8,59,1,3]
console.log(maxValue) //60
Advertisement