lundi 19 juillet 2021

How to filter the array of objects using ES5? [duplicate]

I have an array of objects which contains large number of currencies details. I want to filter based on only few currencies particularly ['USD', 'EUR', 'GBP', 'AED', 'BHD', 'KWD', 'OMR', 'SAR'] also in the same order if possible I mean first USD then EUR and soon. So the filtered array will contain the first object with details of currency USD then second object will contain details of currency EUR and soon (I hope that make sense).

This I will get from api response it's a very long array for simplicity I have just shown few objects(currencies details) inside it.

  var exchangeRates = [
    {
        "CashBuyRate": ".97",
        "CashSellRate": ".9999",
        "MidRate": "0.98495",
        "TransferBuyRate": ".985838961",
        "TransferMidRate": "0.992540582",
        "TransferSellRate": ".999242202",
        "CurAmt": {
            "Currency": "AED",
            "CurrencyName": "United Arab Emirates Dirhams",
            "NumOfDecimals": "2"
        }
    },
    {
        "CashBuyRate": ".00001",
        "CashSellRate": ".00001",
        "MidRate": "0.00001",
        "TransferBuyRate": "2.829008",
        "TransferMidRate": "2.8721515",
        "TransferSellRate": "2.915295",
        "CurAmt": {
            "Currency": "AUD",
            "CurrencyName": "Australian Dollars",
            "NumOfDecimals": "2"
        }
    },
    {
        "CashBuyRate": ".00001",
        "CashSellRate": ".00001",
        "MidRate": "0.00001",
        "TransferBuyRate": ".041325495",
        "TransferMidRate": "0.042879383",
        "TransferSellRate": ".04443327",
        "CurAmt": {
            "Currency": "BDT",
            "CurrencyName": "Bangladesh Taka",
            "NumOfDecimals": "2"
        }
    },
    {
        "CashBuyRate": "9.52",
        "CashSellRate": "9.833931258",
        "MidRate": "9.676965629",
        "TransferBuyRate": "9.576662622",
        "TransferMidRate": "9.67029694",
        "TransferSellRate": "9.763931258",
        "CurAmt": {
            "Currency": "BHD",
            "CurrencyName": "Bahraini Dinar",
            "NumOfDecimals": "3"
        }
    },
    {
        "CashBuyRate": ".00001",
        "CashSellRate": ".00001",
        "MidRate": "0.00001",
        "TransferBuyRate": "2.80080657",
        "TransferMidRate": "2.889783422",
        "TransferSellRate": "2.978760274",
        "CurAmt": {
            "Currency": "CAD",
            "CurrencyName": "Canadian Dollar",
            "NumOfDecimals": "2"
        }
    }
  ]

I want an array of objects similar in structure but only with those currencies mentioned above. And I can't use ES6 which is causing me the trouble. Please suggest some ES5 solutions. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire