我有一个嵌套的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。我该怎么办?
答案 0 :(得分:1)
在您的用例代码下面找到
'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
答案 1 :(得分:1)
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
答案 2 :(得分:1)
关于使用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);
答案 3 :(得分:1)
您可以使用array#reduce
来获取value
数组中values
的最小值和最大值。遍历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);