data = [
{
"group": "banana",
"Nitrogen": "12",
"normal": "15",
"stress": "1"
},
{
"group": "poacee",
"Nitrogen": "6",
"normal": "6",
"stress": "20"
},
{
"group": "sorgho",
"Nitrogen": "11",
"normal": "28",
"stress": "12"
},
{
"group": "triticum",
"Nitrogen": "19",
"normal": "20",
"stress": "12"
}
];我在分组条形图中使用了这个数组。我想从所有的值中得到最大值,也就是这个值中的28。
发布于 2022-07-11 11:35:31
作为其他答案的另一种选择,您可以根据normal属性将数组从最高到最低排序,并选择第一个元素:
data.sort((a, b) => parseInt(b.normal, 10) - parseInt(a.normal, 10));
console.log(data[0]); // The element with the highest normal value
console.log(data[0].normal) // Or just the highest normal value发布于 2022-07-11 11:26:44
我建议您从normal属性中获得最大值。
const data = [
{
"group": "banana",
"Nitrogen": "12",
"normal": "15",
"stress": "1"
},
{
"group": "poacee",
"Nitrogen": "6",
"normal": "6",
"stress": "20"
},
{
"group": "sorgho",
"Nitrogen": "11",
"normal": "28",
"stress": "12"
},
{
"group": "triticum",
"Nitrogen": "19",
"normal": "20",
"stress": "12"
}
];
const max = data.map(x => +x.normal).reduce((a, b) => a > b ? a : b, 0);
console.log(`Max: ${max}`);
发布于 2022-07-11 11:35:47
只需从数组中的所有对象中提取所有数字值,将它们连接到一个集合数组中,然后找出最大值。
const data = [
{
"group": "banana",
"Nitrogen": "12",
"normal": "15",
"stress": "1"
},
{
"group": "poacee",
"Nitrogen": "6",
"normal": "6",
"stress": "20"
},
{
"group": "sorgho",
"Nitrogen": "11",
"normal": "28",
"stress": "12"
},
{
"group": "triticum",
"Nitrogen": "19",
"normal": "20",
"stress": "12"
}
];
const getMax = (arr) => {
const op = arr.reduce((acc, cur) => {
const vals = Object.values(cur).map(v => +v).filter(v => v);
return acc.concat(vals);
}, []);
return Math.max.apply(null, op);
}
console.log(getMax(data)); // 28https://stackoverflow.com/questions/72937719
复制相似问题