小编ste*_*ute的帖子

lodash SortBy object content

I have an array of objects like so:

var quantityPricing = [ 
    { quantity: 1, cost: 0.5 }, 
    { quantity: 100, cost: 0.45 }, 
    { quantity: 1000, cost: 0.25 }, 
    { quantity: 500, cost: 0.35 }
];
Run Code Online (Sandbox Code Playgroud)

我试图根据数量以升序对这个数组的内容进行排序,所以我期望的结果应该是:

[
    { quantity: 1, cost: 0.5 },
    { quantity: 100, cost: 0.45 },
    { quantity: 500, cost: 0.35 },
    { quantity: 1000, cost: 0.25 }
]
Run Code Online (Sandbox Code Playgroud)

因此,我尝试使用lodash命令:

_.sortBy(quantityPricing, ['quantity']);
Run Code Online (Sandbox Code Playgroud)

但是不幸的是,该函数返回的结果似乎只按数量的第一位排序,例如:

{
    "quantity": 1,
    "cost": 0.5
},
{
    "quantity": 100,
    "cost": 0.45
}, …
Run Code Online (Sandbox Code Playgroud)

javascript node.js lodash

1
推荐指数
1
解决办法
7242
查看次数

标签 统计

javascript ×1

lodash ×1

node.js ×1