vue中查找多个对象里的某一个值的最大值该怎么写?从后台获取到的数据对象为CategoryListvalue要计算其中表里最大值的列名是orderNum
在Vue中,可以使用Array.prototype.reduce()方法来查找多个对象中某一个值的最大值。具体的写法如下:
// 假设从后台获取到的数据对象为CategoryList.value
let CategoryList = {
value: [
{ orderNum: 10 },
{ orderNum: 20 },
{ orderNum: 30 },
// ...
]
};
// 使用reduce方法找到orderNum的最大值
let maxOrderNum = CategoryList.value.reduce((max, obj) => {
return (obj.orderNum > max) ? obj.orderNum : max;
}, 0);
console.log(maxOrderNum); // 输出最大的orderNum值
在上面的代码中,使用reduce()方法遍历CategoryList.value数组,初始值为0。在每次迭代中,比较当前对象的orderNum值与之前的最大值max,如果当前对象的orderNum值更大,则将其作为新的最大值返回,否则保持原有的最大值不变。最终,maxOrderNum存储的就是orderNum的最大值
原文地址: https://www.cveoy.top/t/topic/ijXv 著作权归作者所有。请勿转载和采集!