【问题标题】:Compare and Filter Object Array in javascript在 javascript 中比较和过滤对象数组
【发布时间】:2019-04-11 04:37:11
【问题描述】:

如何确定最便宜和最快的费率并获得单个对象的价值。

  • cheapest 是通过使用 netfeeleast value 来确定的
  • fastest 是通过使用具有 less daysspeed 确定的
  • best 是通过使用具有highest valueamount 确定的

我被卡住了,让我知道是否有任何替代解决方案。

var result = getValue(obj);
getValue(obj){
 var cheapest= Math.min.apply(Math, obj.map(function (el) {
        return el.netfee;
  })); 
  var best= Math.max.apply(Math, obj.map(function (el) {
        return el.amount;
  }));
  var res= Object.assign({}, cheapest, best);
return res;
}
var obj=[
{ 
  id: "sample1",
  netfee: 10,
  speed: "1days",
  amount: "100"
},
{
 id: "sample2",
 netfee: 6,
 speed: "2days",
 amount: "200"
},
{
 id: "sample3",
 netfee: 4,
 speed: "3days",
 amount: "50"
}
]

Expected Output:

Cheapest : Sample 3

Fastest: Sample 1

Best: Sample 2

【问题讨论】:

    标签: javascript jquery arrays object


    【解决方案1】:

    这么简单..

    var obj=[
      { id: "sample1", netfee: 10, speed: "1days", amount: "100" },
      { id: "sample2", netfee: 6,  speed: "2days", amount: "200" },
      { id: "sample3", netfee: 4,  speed: "3days", amount:  "50" }
    ];
    
    var
      cheapest = obj.reduce((acc, cur)=>(acc.netfee < cur.netfee ? acc : cur)).id,
      fastest  = obj.reduce((acc, cur)=>(parseInt(acc.speed,10) < parseInt(cur.speed,10) ? acc : cur)).id,
      best     = obj.reduce((acc, cur)=>(Number(acc.amount) > Number(cur.amount) ? acc : cur)).id;
    
    console.log( "cheapest =", cheapest  )
    console.log( "fastest  =", fastest  )
    console.log( "best     =", best  )

    [编辑]: 感谢 muka.gergelyparseInt(acc.speed,10) 的评论(指定使用基数 10)
    备忘:console.log(parseFloat('0.7 days') return = 0.7

    【讨论】:

    • 不要在没有设置基数的情况下使用 parseInt,例如 parseInt(acc.speed, 10)。它为您节省了大量的调试时间和“到底发生了什么!?”输入问题。
    • @muka.gergely 我想如果 parseInt 方法的 string 参数以 0x0,那么radix参数默认为10? ?️
    • 可能是这种情况,但没有什么能确保在 sn-p 中 - 您可以有 0.5 天的速度(在此代码允许的范围内),这比抱歉更安全 :)跨度>
    • 哦,是的,你有理由 :)
    【解决方案2】:

    您可以应用这个技巧,得到预期输出中的答案:

    var obj = [
      {
        id: "sample1",
        netfee: 10,
        speed: "1days",
        amount: "100"
      },
      {
        id: "sample2",
        netfee: 6,
        speed: "2days",
        amount: "200"
      },
      {
        id: "sample3",
        netfee: 4,
        speed: "3days",
        amount: "50"
      }
    ];
    
    var result = getValue(obj);
    
    function getValue(obj) {
      var cheapest = obj.reduce((acc, next) => acc.netfee < next.netfee ? acc : next).id;
      var fastest = obj.reduce((acc, next) => parseInt(acc.speed) < parseInt(next.speed) ? acc : next).id;
      var best = obj.reduce((acc, next) => +acc.amount > +next.amount ? acc : next).id;
    
      var res = Object.assign({}, {
        cheapest,
        fastest,
        best
      });
    
      return res;
    }
    
    console.log(result);

    【讨论】:

    • return { cheapest, fastest, best }; 做同样的事情 ;)
    • 获取信息 +acc.amountNumber(acc.amount)
    • @MrJ 真的吗??好吧 jsperf.com 告诉我否则,请查看here
    • ?我不认为这种性能测试在这种情况下是有效的。这个测试应该在少于 10 个循环的情况下完成,并使用表格中的数字,并使用负值;所以我们可以更好地判断解释器 JS 的工作,而它不能启动操作优化器
    • 哦,是的,我理解你的观点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2020-08-27
    相关资源
    最近更新 更多