Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

349
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda