本文介绍了在对象数组中的属性中查找具有最大值的所有对象,并从同一对象返回其他属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有类似的问题,但使用这些方法只返回一个最大值.我需要的是确定数组的哪些对象在给定属性中具有最大值,并在给定属性中具有最大值的那些对象中返回某个 (other) 属性的值.

我有这个名为 week 的对象数组,有两个属性 "name""traffic" :

[{名称:星期六",交通:12},{名称:星期日",交通:12},{名称:星期一",交通:13},{名称:星期二",交通:9},{名称:星期三",交通:10},{名称:星期四",交通:8},{名称:星期五",交通:13},]


在这种情况下,星期一星期五具有属性交通"的最大值13,我需要一种方法来返回一个 字符串,其中包含最高 "Traffic" value 如果只有一天,并且包含名称 (作为字符串) 的数组具有最高 流量" 价值的天数,如果有超过一天具有最高的流量" value,因为在这种情况下将返回一个包含 MondayFrid​​ay 的数组.

我已经试过了:

function getMaxTr() {return week.reduce((max, p) => p.traffic > max ?p.traffic : max, week[0].traffic);}

但是有了这个,我只得到了属性traffic"的一个最大值,即13.

还有这个:

let max = week [week.length - 1];

通过最后一个,我得到了一个具有最大流量值的对象,如下所示:

Object { name: "Friday", traffic: 13 }

解决方案

可以使用reduce.在每次迭代中,检查结果中是否存在具有较低或相等 traffic 属性的元素,如果是,则替换前一种情况的整个内容,或将相等元素添加到结果中.如果以上都不返回真,只需再次返回最后一次迭代的元素.

const arr = [{名称:星期六",交通:12},{名称:星期日",交通:12},{名称:星期一",交通:13},{名称:星期二",交通:9},{名称:星期三",交通:10},{名称:星期四",交通:8},{名称:星期五",交通:13},];让 res = arr.reduce((a, b) => {让现在 = a.pop();if (now.traffic < b.traffic) return [b];if (now.traffic === b.traffic) return [...a, now, b];返回 [...a,现在];}, [arr[0]]).map(e => e.name);res = res.length >1 ?资源:资源[0];console.log(res);

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" :

[
 { 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.

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.

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 }

解决方案

You can use 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. If none of above returns true, simply return the last iteration's element again.

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);

这篇关于在对象数组中的属性中查找具有最大值的所有对象,并从同一对象返回其他属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:00