_.sortBy() 从浮点值中移除额外的 0(转换 0.10 -> 0.1 等等)

_.sortBy() removing extra 0 from floating point value (converting 0.10 -> 0.1 and so on)

下面是我要排序的数组 -

var baseBetAmount = [
{
    val: 'OtherBaseBet',
    text: 'Other'
},
{
    val: 0.10,
    text: '[=11=].10'
},
{
    val: 0.20,
    text: '[=11=].20'
},
{
    val: 0.50,
    text: '[=11=].50'
},
{
    val: 1,
    text: ''
},
{
    val: 2,
    text: ''
}]

我正在写下面的功能来对其进行排序。

  var options=  _.sortBy(baseBetAmount)

返回低于输出 -

[
{
    val: 'OtherBaseBet',
    text: 'Other'
},
{
    val: 0.1,
    text: '[=13=].10'
},
{
    val: 0.2,
    text: '[=13=].20'
},
{
    val: 0.5,
    text: '[=13=].50'
},
{
    val: 1,
    text: ''
},
{
    val: 2,
    text: ''
}]

此处 0.10 替换为 0.1 ,0.50 替换为 0.5 等等。 任何人都可以在这里帮助我,我不希望 0.10 被 0.1 替换等等

这是一个可行的解决方案,但在进行数学计算时需要解析值。

var baseBetAmount = [
{
    val: 'OtherBaseBet',
    text: 'Other'
},
{
    val: 0.10,
    text: '[=10=].10'
},
{
    val: 0.20,
    text: '[=10=].20'
},
{
    val: 0.50,
    text: '[=10=].50'
},
{
    val: 1,
    text: ''
},
{
    val: 2,
    text: ''
}];

 var options = _.sortBy(baseBetAmount).map(function(base){
  if(_.isNumber(base.val)){
   base.val = base.val.toFixed(2);
  }
  return base;
 });

console.log(options);
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
 
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script>
</body>
</html>

toFixed() returns 一个字符串,所以你必须使用 parseFloat 解析它进行数学计算。