获取嵌套 JSON 对象的最小值和最大值
Get min and max of nested JSON object
我有一个嵌套的 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},
]}]
我想找出值中的最小值和最大值。就像在这种情况下,它将是最小值 0 和最大值 194。我应该怎么做?
在下面找到您的用例代码,
'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
使用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);
您可以使用array#reduce
获取values
数组中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);
我有一个嵌套的 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},
]}]
我想找出值中的最小值和最大值。就像在这种情况下,它将是最小值 0 和最大值 194。我应该怎么做?
在下面找到您的用例代码,
'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
使用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);
您可以使用array#reduce
获取values
数组中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);