我知道这里也有类似的问题,但是使用这些方法,只会返回一个最大值。我需要确定数组的哪些对象在给定属性中具有最大值,并在给定属性具有最大值的那些对象内返回某个(other)属性的值。
我有一个名为 week 的对象数组,具有两个属性“ name” 和“ traffic” :
[
{ name: "Saturday", traffic: 12 },
{ name: "Sunday", traffic: 12 },
{ name: "Monday", traffic: 13 },
{ name: "Tuesday", traffic: 9 },
{ name: "Wednesday", traffic: 10 },
{ name: "Thursday", traffic: 8 },
{ name: "Friday", traffic: 13 },
]
在这种情况下, 星期一 和 星期五 具有属性“ “流量””的最大值 ,它是 13 ,我需要一种方法来返回 string ,其中包含日期最高的< strong> “流量” <值>值(如果只有一天的话),以及包含名称的(作为字符串)的数组 “流量” 值最高的天,如果有超过一天的 “流量” value ,因为在这种情况下将返回包含 Monday 和 Friday 的数组。
我已经尝试过了:
function getMaxTr() {
return week.reduce((max, p) => p.traffic > max ?
p.traffic : max, week[0].traffic);
}
但是,我只有一个最大的属性“ traffic” 值为 13 。
这:
let max = week [week.length - 1];
最后一个得到一个具有最大流量值的对象,如下所示:
对象{名称:“星期五”,访问量:13}
答案 0 :(得分:3)
您可以使用reduce。在每次迭代中,请检查结果中是否存在一个具有较低或相等的traffic
属性的元素,如果是,则将整个情况替换为前一种情况,或者在结果中添加equal元素。如果以上都不返回true,则只需再次返回上一次迭代的元素。
const arr = [
{ name: "Saturday", traffic: 12 },
{ name: "Sunday", traffic: 12 },
{ name: "Monday", traffic: 13 },
{ name: "Tuesday", traffic: 9 },
{ name: "Wednesday", traffic: 10 },
{ name: "Thursday", traffic: 8 },
{ name: "Friday", traffic: 13 },
];
let res = arr.reduce((a, b) => {
let now = a.pop();
if (now.traffic < b.traffic) return [b];
if (now.traffic === b.traffic) return [...a, now, b];
return [...a, now];
}, [arr[0]]).map(e => e.name);
res = res.length > 1 ? res : res[0];
console.log(res);
答案 1 :(得分:2)
好吧,如果要返回最大值为name
的对象traffic
,可以使用 Array#filter()
, { {3}} 和 Array#reduce()
这样的方法:
let maxTraffic = arr.reduce(function(a, b) {
return a.traffic > b.traffic ? a.traffic : b.traffic;
});
var result = arr.filter(a => a.traffic == maxTraffic).map(a => a.name);
这将返回一个array
,其中包含最大值为traffic
的元素的名称。
演示:
这是一个有效的演示:
var arr = [
{ name: "Saturday", traffic: 12 },
{ name: "Sunday", traffic: 12 },
{ name: "Monday", traffic: 13 },
{ name: "Tuesday", traffic: 9 },
{ name: "Wednesday", traffic: 10 },
{ name: "Thursday", traffic: 8 },
{ name: "Friday", traffic: 13 },
];
let maxTraffic = arr.reduce(function(a, b) {
return a.traffic > b.traffic ? a.traffic : b.traffic;
});
var result = arr.filter(a => a.traffic == maxTraffic).map(a => a.name);
console.log(result);
答案 2 :(得分:2)
如果reduce数组仅包含一个元素,则可以使用函数reduce
对日期进行分组,而函数pop
可以进行分组。
var array = [ { name: "Saturday", traffic: 12 }, { name: "Sunday", traffic: 12 }, { name: "Monday", traffic: 13 }, { name: "Tuesday", traffic: 9 }, { name: "Wednesday", traffic: 10 }, { name: "Thursday", traffic: 8 }, { name: "Friday", traffic: 13 }],
reduced = array.reduce((a, {name, traffic}) => {
if (traffic > a.highest) {
a.current = [name];
a.highest = traffic;
} else if (traffic === a.highest) a.current.push(name);
return a;
}, {highest: 0, current: []}).current,
result = reduced.length === 1 ? reduced.pop() : reduced;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
当样本仅包含一个具有最高流量值的对象时,此代码段将结果显示为String:
var array = [ { name: "Saturday", traffic: 12 }, { name: "Sunday", traffic: 12 }, { name: "Monday", traffic: 1 }, { name: "Tuesday", traffic: 9 }, { name: "Wednesday", traffic: 10 }, { name: "Thursday", traffic: 8 }, { name: "Friday", traffic: 13 }],
reduced = array.reduce((a, {name, traffic}) => {
if (traffic > a.highest) {
a.current = [name];
a.highest = traffic;
} else if (traffic === a.highest) a.current.push(name);
return a;
}, {highest: 0, current: []}).current,
result = reduced.length === 1 ? reduced.pop() : reduced;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }