admin管理员组

文章数量:1415467

I have a nested json object which looks like---

      [
      {"key":"AXCG","values":[
        {"interval":'1_to_2years',"value":34},
        {"interval":'3_to_4years',"value":12},
        {"interval":'5_to_6years',"value":45},
      ]},
      {"key":"BDGT","values":[
        {"interval":'1_to_2years',"value":194},
        {"interval":'3_to_4years',"value":12},
        {"interval":'5_to_6years',"value":45},
      ]},
{"key":"YTEF","values":[
        {"interval":'1_to_2years',"value":0},
        {"interval":'3_to_4years',"value":12},
        {"interval":'5_to_6years',"value":15},
      ]}]

I want to find the min and max among the value. Like in this case it would be min 0 and 194 maximum. How should I do it?

I have a nested json object which looks like---

      [
      {"key":"AXCG","values":[
        {"interval":'1_to_2years',"value":34},
        {"interval":'3_to_4years',"value":12},
        {"interval":'5_to_6years',"value":45},
      ]},
      {"key":"BDGT","values":[
        {"interval":'1_to_2years',"value":194},
        {"interval":'3_to_4years',"value":12},
        {"interval":'5_to_6years',"value":45},
      ]},
{"key":"YTEF","values":[
        {"interval":'1_to_2years',"value":0},
        {"interval":'3_to_4years',"value":12},
        {"interval":'5_to_6years',"value":15},
      ]}]

I want to find the min and max among the value. Like in this case it would be min 0 and 194 maximum. How should I do it?

Share Improve this question edited Dec 7, 2018 at 8:42 IndexOutOfDevelopersException 1,3547 gold badges15 silver badges28 bronze badges asked Dec 6, 2018 at 10:08 user435215user435215 1373 silver badges11 bronze badges 3
  • 2 The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a minimal reproducible example. For more information, please see How to Ask and take the tour. – CertainPerformance Commented Dec 6, 2018 at 10:11
  • Array.reduce is what you are looking for. – Vinz243 Commented Dec 6, 2018 at 10:14
  • As a side remark, there is no such thing as "JSON object" (or "JSON array"). JSON is a text representation of some data structure. The format of JSON resembles the JavaScript language but it's not linked to JavaScript in any way. JSON can be handled in many languages. It is not clear from your question if what you posted is a JSON or a JavaScript array of objects. Since it is not valid JSON I assume it is a JavaScript fragment and the tag json attached to the question is incorrect. – axiac Commented Dec 6, 2018 at 10:41
Add a ment  | 

4 Answers 4

Reset to default 2

Find below the code for your use-case,

'use strict'
const collection = [
    {
        "key": "AXCG", "values": [
            { "interval": '1_to_2years', "value": 34 },
            { "interval": '3_to_4years', "value": 12 },
            { "interval": '5_to_6years', "value": 45 },
        ]
    },
    {
        "key": "BDGT", "values": [
            { "interval": '1_to_2years', "value": 194 },
            { "interval": '3_to_4years', "value": 12 },
            { "interval": '5_to_6years', "value": 45 },
        ]
    },
    {
        "key": "YTEF", "values": [
            { "interval": '1_to_2years', "value": 0 },
            { "interval": '3_to_4years', "value": 12 },
            { "interval": '5_to_6years', "value": 15 },
        ]
    }]
const list = []
collection.every(e => e.values.every(e2 => list.push(e2.value)));

console.log('Max Value:: ' + Math.max.apply(null, list)); // 194
console.log('Min Value:: ' + Math.min.apply(null, list)); // 0

var collection = [
    {
        "key": "AXCG", "values": [
            { "interval": '1_to_2years', "value": 34 },
            { "interval": '3_to_4years', "value": 12 },
            { "interval": '5_to_6years', "value": 45 },
        ]
    },
    {
        "key": "BDGT", "values": [
            { "interval": '1_to_2years', "value": 194 },
            { "interval": '3_to_4years', "value": 12 },
            { "interval": '5_to_6years', "value": 45 },
        ]
    },
    {
        "key": "YTEF", "values": [
            { "interval": '1_to_2years', "value": 0 },
            { "interval": '3_to_4years', "value": 12 },
            { "interval": '5_to_6years', "value": 15 },
        ]
    }
];


var values = [];

collection.forEach(function (item) {
    item.values.forEach(function (nestedItem) {
        values.push(nestedItem.value);
    });
});

console.log("Min:" + Math.min.apply(Math, values)); // Min:0
console.log("Max:" + Math.max.apply(Math, values)); // Max:194

Another variation on the simple idea of using Array.forEach:

    var o = [
        {
            "key": "AXCG", "values": [
                { "interval": '1_to_2years', "value": 34 },
                { "interval": '3_to_4years', "value": 12 },
                { "interval": '5_to_6years', "value": 45 },
            ]
        },
        {
            "key": "BDGT", "values": [
                { "interval": '1_to_2years', "value": 194 },
                { "interval": '3_to_4years', "value": 12 },
                { "interval": '5_to_6years', "value": 45 },
            ]
        },
        {
            "key": "YTEF", "values": [
                { "interval": '1_to_2years', "value": 0 },
                { "interval": '3_to_4years', "value": 12 },
                { "interval": '5_to_6years', "value": 15 },
            ]
        }];
    var minimum = 9999;
    var maximum = 0;


    o.forEach(function (element) {
        var inner = element.values;
        inner.forEach(function (innerELement) {
            if (innerELement.value < minimum) minimum = innerELement.value;
            if (innerELement.value > maximum) maximum = innerELement.value;
        });
    });


    console.log('Min is ' + minimum + ' and max is ' + maximum);

You can use array#reduce to get the minimum and maximum value of the value inside the values array. Iterate through each object of values array and pare the values with minimum and maximum value stored and when you encounter new minimum and maximum value update the stored value.

var collection = [{ "key": "AXCG", "values": [{ "interval": '1_to_2years', "value": 34 }, { "interval": '3_to_4years', "value": 12 }, { "interval": '5_to_6years', "value": 45 }, ] }, { "key": "BDGT", "values": [{ "interval": '1_to_2years', "value": 194 }, { "interval": '3_to_4years', "value": 12 }, { "interval": '5_to_6years', "value": 45 }, ] }, { "key": "YTEF", "values": [{ "interval": '1_to_2years', "value": 0 }, { "interval": '3_to_4years', "value": 12 }, { "interval": '5_to_6years', "value": 15}, ] } ],
    result = collection.reduce((r,{values}) => {
      values.forEach(({value}) => {
        r.min = r.min > value ? value : r.min;
        r.max = r.max < value ? value : r.max;
      });
      return r;
    },{min: Number.MAX_SAFE_INTEGER, max: Number.MIN_SAFE_INTEGER});
console.log(result);

本文标签: javascriptGet min and max of nested JSON objectStack Overflow