Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

348
Views
how to get max value from a nested json array

I have a nested json array and I am trying to get the maximum value of the points attribute in this array.

 data = {
        "name": "KSE100",
        "children": [
            {
                "name": "TECHNOLOGY & COMMUNICATION",
              
                "children": [
                    {
                        "name": "TRG",
                       
                        'points': -21
                    },
                    {
                        "name": "SYS",
                    
                   
                    },

                ]
            },
            {
                "name": "OIL",
             
                "children": [
                    {
                        "name": "PPL",
                       
                        'points': 9
                    },
                    {
                        "name": "PSO",
                        
                        'points': -19
                    },

                ]
            },


        ]

    }

I want the max value of points from under the children sections. I mean from under technology and oil sectors.

What I've done so far:

var max;

for (var i in data.children.length) {
  for (var j in data.data[i]) {
    var point = data.data[i].children[j]
  }
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try the following:

data = {
        "name": "KSE100",
        "children": [
            {
                "name": "TECHNOLOGY & COMMUNICATION",
              
                "children": [
                    {
                        "name": "TRG",
                       
                        'points': -21
                    },
                    {
                        "name": "SYS",
                    
                   
                    },

                ]
            },
            {
                "name": "OIL",
             
                "children": [
                    {
                        "name": "PPL",
                       
                        'points': 9
                    },
                    {
                        "name": "PSO",
                        
                        'points': -19
                    },

                ]
            },


        ]

    }

var array = [];

for (var first of data.children) {
  for (var second of first.children) {
    if(second.points != undefined)
    {
      array.push(second);
    }
  }
}

var maximumValue = Math.max.apply(Math, array.map(function(obj) { return obj.points; }));

console.log(maximumValue);

about 4 years ago · Juan Pablo Isaza Report

0

I wasn't 100% sure, what your exact goal is, so I included a grouped max value and and overall max value with a slight functional approach.

Please be aware that some functionalities are not working in older browsers i.e. flatMap. This should anyways help you get started and move on.

const data = {
  name: "KSE100",
  children: [
    {
      name: "TECHNOLOGY & COMMUNICATION",
      children: [
        {
          name: "TRG",
          points: -21,
        },
        {
          name: "SYS",
        },
      ],
    },
    {
      name: "OIL",
      children: [
        {
          name: "PPL",
          points: 9,
        },
        {
          name: "PSO",
          points: -19,
        },
      ],
    },
  ],
};

const maxPointsByGroup = data.children.reduce(
  (acc, entry) => [
    ...acc,
    {
      name: entry.name,
      max: Math.max(
        ...entry.children
          .map((entry) => entry.points)
          .filter((entry) => typeof entry === "number")
      ),
    },
  ],
  []
);
console.log("grouped max:", maxPointsByGroup);

const overallMax = Math.max(
  ...data.children
    .flatMap((entry) => entry.children.flatMap((entry) => entry.points))
    .filter((entry) => typeof entry === "number")
);
console.log("overall max:", overallMax);

about 4 years ago · Juan Pablo Isaza Report

0

you can use the reduce method on the array object to do this

const maxValues = []
data.children.forEach(el => {
   if (el.name === 'OIL' || el.name === 'TECHNOLOGY & COMMUNICATIO'){
      const max = el.children.reduce((current, previous) => {
          if (current.points > previous.points) {
              return current
          }
      }, 0)
      maxValues.append({name: el.name, value: max.points})
   }
})

This will give you an array of the objects with the name and max value.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!