-2

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?

user435215
  • 127
  • 2
  • 11
  • 2
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), 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 [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Dec 06 '18 at 10:11
  • Array.reduce is what you are looking for. – Vinz243 Dec 06 '18 at 10:14
  • As a side remark, there is no such thing as "JSON object" (or "JSON array"). [JSON](http://json.org) 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 Dec 06 '18 at 10:41

4 Answers4

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
Umair Anwaar
  • 1,130
  • 9
  • 27
Sanket
  • 945
  • 11
  • 24
2

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

Umair Anwaar
  • 1,130
  • 9
  • 27
1

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);
Spyros P.
  • 296
  • 3
  • 14
1

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 compare 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);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51