0

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 ?

2
  • What have you tried so far? What about using .flatMap and .sort? Commented Sep 2, 2021 at 13:24
  • You should have tried something and then add a question here. There will be a lot of questions related to it on the internet. Commented Sep 2, 2021 at 13:26

5 Answers 5

1

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

Comments

1

You can just browse trough it, like you would with any other 2d Object-Array. You have 2 for loops. One loops trought Application itself, another loops trough chartData. The code looks as follows:

let maxVal = 0
for (let i = 0; i < Application.length; i++) {
  for (let j = 0; j < Application[i].chartData.length; j++) {
    if (Application[i].chartData[j].value > maxVal) {
      maxVal = Application[i].chartData[j].value
    }
  }
}
console.log(maxVal)

Comments

1

You can flatten your array of values and use Math.max() to find out the maximum value.

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", }, ],
      maximum = Math.max(...application.flatMap(({chartData}) => chartData)
                          .map(({value}) => value));
console.log(maximum);

Comments

0

Here my solution, it can be done some improvements, hope it helps you

import {reduce } from 'ramda'

const maxValue = reduce((max, {value}) => value > max ? value : max, -Infinity)
const maximunValue = reduce((acc, {chartData}) => maxValue(chartData) > acc ? maxValue(chartData) : acc, -Infinity)

maximunValue(Application) // function returns 60

The solution in ramda documentation here

Comments

0

You could use the combination of flatMap along with Math.max to get the maximum value.flatMap flattens the array to a depth of 1, and then use Math.max to get the maximum value out of it.

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", }, ]
const result = Math.max(
  ...Application.flatMap(({ chartData }) => chartData.map(({ value }) => value))
);

console.log(result);

1 Comment

What about using flatMap?

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.