简体   繁体   English

在对象数组中的属性中查找具有最大值的所有对象,并从同一对象返回其他属性的值

[英]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.

相关问题 在对象数组中查找相同的属性值 - Finding the same property value in an array of objects 通过相同属性的值按其他对象过滤对象数组 - Filter array of objects by other object via value of same property 对象数组的对象。 获取具有相同属性的所有值的长度 - Object of array of objects. Get the length of all values with the same property 使用属性值从对象数组中查找并返回 object - Find and Return an object from an array of array of objects using a property value 检查对象数组中的所有属性值是否相同 - Check if all property values are the same in an Array of Objects 如何将具有相同属性名称的对象数组转换为从给定数组中的值连接值的对象? JS - How to transform an array of objects with the same property name into the object in which value is concatenated from values in a given array? JS 在一个对象数组中,将所有先前的对象修改为具有特定属性的 object - Within an array of objects modify all previous objects to an object with a specific property 查找对象数组中的最大值,以及属性值 &gt; 0 的所有数字 object 属性 - Find the max value in an array of objects, and all numeric object properties with property value > 0 如何获取具有相同属性的对象并在数组中具有最大值 - Javascript - how to get objects with the same property and has the max value in array - Javascript 将对象的属性值添加到对象数组 - Add property value from object to array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM