【问题标题】:Finding all objects with Max value in a property within an Array of Objects and return values of other property from the same object在对象数组中的某个属性中查找所有具有最大值的对象,并从同一对象返回其他属性的值
【发布时间】:2018-12-26 12:57:26
【问题描述】:

我知道这里有类似的问题,但使用这些方法只返回一个最大值。我需要确定数组的哪些对象在给定属性中具有该最大值,并在给定属性中具有最大值的那些对象中返回某个 (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 },
]


在这种情况下,MondayFrid​​ay 具有属性 “Traffic”的最大值 这是 13 我需要一种方法来返回一个 string 包含最高的一天的名称strong>"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 }

【问题讨论】:

  • 你试过什么?请发布您的代码。

标签: javascript arrays object


【解决方案1】:

如果你想返回最大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);

这将返回一个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】:

    如果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; }

    当样本仅包含一个流量值最高的对象时,此代码 sn-p 将结果显示为字符串:

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

    【讨论】:

      【解决方案3】:

      你可以使用reduce。在每次迭代中,检查结果中是否存在具有较低或相等 traffic 属性的元素,如果是,则替换前一种情况的整个内容,或将相等元素添加到结果中。如果以上都没有返回 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);

      【讨论】:

      • 可能是最好的算法,但a few checks and return the right thing不是解释。
      猜你喜欢
      • 2014-05-07
      • 2018-07-09
      • 1970-01-01
      • 2013-10-14
      • 2019-09-14
      • 1970-01-01
      • 2022-07-08
      • 2012-10-26
      相关资源
      最近更新 更多