簡體   English   中英

如何使用lodash基於值對嵌套數組進行排序?

[英]How to sort a nested array based on value with 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另一個問題是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"
            },
        ]
    },
]

非常感謝您的善意,當然還有您的時間。

_.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 });
});

不需要像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); 

並通過緩存價格查詢進行優化,否則將多次對每個項目執行價格查詢。

 const 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" }, ] }, ]; function sortResults(results, curr) { return results .map(result => ([result, result.prices.find(price => price.currency === curr).amount - 0])) .sort((a, b) => a[1] - b[1]) .map(res => res[0]); } console.log(sortResults(results, "USD").map(res => res.name)); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM