[英]Finding all objects with Max value in a property within an Array of Objects and return values of other property from the same object
I know there are similar questions here, but with those methods only one max value is returned. 我知道这里有类似的问题,但是使用这些方法只返回一个最大值。 What I need is to determine which objects of the array have that max value in a given property and return the value of a certain (other) property within those objects that have the max value in the given property. 我需要的是确定数组中哪些对象在给定属性中具有该最大值,并返回在给定属性中具有最大值的那些对象中的某个(其他)属性的值。
I have this array of objects called week with two properties "name" and "traffic" : 我有这个名为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 },
]
In this case Monday and Friday have the max value for the property "Traffic" which is 13 and I need a way to return a string containing the name of the day with highest "Traffic" value if there is only one day, and an array containing the names (as strings) of the days that have highest "Traffic" value if there are more than one day with highest "Traffic" value , as in this case would return an array containing Monday and Friday . 在这种情况下, 星期一和星期五的属性“流量”的最大值为13 ,我需要一种方法来返回一个字符串 ,如果只有一天,则返回包含具有最高“流量” 值的日期的名称,以及一个数组如果有超过一天具有最高“流量” 值的日期,则包含具有最高“流量” 值的日期的名称(作为字符串) ,因为在这种情况下将返回包含星期一和星期五的数组。
I have tried this: 我试过这个:
function getMaxTr() {
return week.reduce((max, p) => p.traffic > max ?
p.traffic : max, week[0].traffic);
}
But with this I only got one max value of the property "traffic" which is 13 . 但有了这个,我只得到了一个属性“流量”的最大值,即13 。
And this: 还有这个:
let max = week [week.length - 1];
With this last one I get one object which has the max traffic value, like this: 使用最后一个,我得到一个具有最大流量值的对象,如下所示:
Object { name: "Friday", traffic: 13 } 对象{名称:“星期五”,流量:13}
You can use reduce. 你可以使用reduce。 In every iteration, check if there's either an element with a lower or an equal traffic
property in your result, if so either replace the whole thing for the former case, or add the equal element to your result. 在每次迭代中,检查结果中是否存在具有较低或相等traffic
属性的元素,如果是,则替换前一种情况的整个事物,或者将相等的元素添加到结果中。 If none of above returns true, simply return the last iteration's element again. 如果以上都不返回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);
Well if you want to return the name
of the objects with max traffic
value, you can use a combination of Array#filter()
, Array#reduce()
and Array#map()
methods like this: 好吧,如果要返回具有最大traffic
值的对象的name
,可以使用Array#filter()
, Array#reduce()
和Array#map()
方法的组合,如下所示:
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);
This will return an array
containing the names of elements with max traffic
value. 这将返回一个array
其中包含具有最大traffic
值的元素的名称。
Demo: 演示:
This is a working demo: 这是一个有效的演示:
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);
You can use the function reduce
to group the days and the function pop
in case the reduce array contains only one element. 如果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; }
This code snippet shows the result as String when the sample contains only one object with the highest traffic value: 当示例仅包含一个流量值最高的对象时,此代码段将结果显示为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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.