如何使用lodash根据值对嵌套数组进行排序?

时间:2019-03-05 03:02:01

标签: javascript arrays sorting filter lodash

我正在学习如何使用lodash库,但是遇到了一个我认为不知道如何解决的问题。我想用lodash对看起来像这样的嵌套数组进行排序:

"results": [
  {
        "id": "12345",
        "name": "toy123",
        "date_created": "2017-08-29T16:10:37Z",
        "date_last_modified": "2019-01-29T17:19:36Z",
        "prices": [
            {
                "currency": "USD",
                "amount": "100.00"
            },
            {
                "currency": "EUR",
                "amount": "88.23"
            },
        ]
    },
    {
        "id": "54321",
        "name": "toy321",
        "date_created": "2017-08-29T16:10:37Z",
        "date_last_modified": "2019-01-29T17:19:36Z",
        "prices": [
            {
                "currency": "USD",
                "amount": "80.00"
            },
            {
                "currency": "EUR",
                "amount": "70.58"
            },
        ]
    },
]

我想基于嵌套在给定数组中的prices数组对数组进行排序。排序将考虑prices.currencyprices.amount并产生如下输出,其中基于USDamount对给定数组进行升序排序。还有一个问题是prices.amount是一个字符串,而不是数字。

[
    {
        "id": "54321",
        "name": "toy321",
        "date_created": "2017-08-29T16:10:37Z",
        "date_last_modified": "2019-01-29T17:19:36Z",
        "prices": [
            {
                "currency": "USD",
                "amount": "80.00"
            },
            {
                "currency": "EUR",
                "amount": "70.58"
            },
        ]
  },
  {
        "id": "12345",
        "name": "toy123",
        "date_created": "2017-08-29T16:10:37Z",
        "date_last_modified": "2019-01-29T17:19:36Z",
        "prices": [
            {
                "currency": "USD",
                "amount": "100.00"
            },
            {
                "currency": "EUR",
                "amount": "88.23"
            },
        ]
    },
]

在此先感谢您的好意,当然还有您的时间。

3 个答案:

答案 0 :(得分:5)

_.sortBy()方法不支持自定义比较器,应改为使用Array.prototype.sort()。您也不需要解析prices.amountString.prototype.localeCompare()可以为您进行比较,它支持带有数字值的字符串。

将它们放在一起,您的实现可能看起来像这样:

results.sort((a, b) => {
    const priceA = _.find(a.prices, { currency: 'USD' });
    const priceB = _.find(b.prices, { currency: 'USD' });
    return priceA.amount.localeCompare(priceB.amount, undefined, { numeric: true });
});

答案 1 :(得分:4)

不需要外部库,例如loadash。

const arr = [
  {
        "id": "12345",
        "name": "toy123",
        "date_created": "2017-08-29T16:10:37Z",
        "date_last_modified": "2019-01-29T17:19:36Z",
        "prices": [
            {
                "currency": "USD",
                "amount": "100.00"
            },
            {
                "currency": "EUR",
                "amount": "88.23"
            },
        ]
    },
    {
        "id": "54321",
        "name": "toy321",
        "date_created": "2017-08-29T16:10:37Z",
        "date_last_modified": "2019-01-29T17:19:36Z",
        "prices": [
            {
                "currency": "USD",
                "amount": "80.00"
            },
            {
                "currency": "EUR",
                "amount": "70.58"
            },
        ]
    },
];

const naturalSort = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare;

arr.sort((a,b) => naturalSort(a.prices.find(p => p.currency === 'USD').amount, b.prices.find(p => p.currency === 'USD').amount));

console.log(arr);

答案 2 :(得分:0)

并通过缓存价格查找进行优化,否则将对每个项目执行多次。

driver.hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done");