简体   繁体   English

获取嵌套 JSON 对象的最小值和最大值

[英]Get min and max of nested JSON object

I have a nested json object which looks like---我有一个嵌套的 json 对象,它看起来像---

      [
      {"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.就像在这种情况下一样,它是最小 0 和最大 194。 How should I do it?我该怎么做?

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:使用 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.您可以使用array#reduce获取values数组中value的最小值和最大值。 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.遍历values数组的每个对象并将这些值与存储的最小值和最大值进行比较,当遇到新的最小值和最大值时更新存储的值。

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM