這個問題在這里已經有了答案: 如何按多個欄位對物件陣列進行排序? (32 個回答) Javascript - 基于另一個陣列對陣列進行排序 (25 個回答) 6 小時前關閉。
如何使用Array.prototype.sort()bytype和currency按此順序對陣列進行排序:
借記 => 貸記 => 外部 => 貸款
如果同一型別有多種貨幣,則按以下順序排列:
盧布 => 美元 => 歐元 => 英鎊
我了解如何對數字使用 sort(),但我不了解如何進行此類排序。
const accounts = [
{ id: 1, type: 'credit', currency: 'GBP' },
{ id: 2, type: 'debit', currency: 'EUR' },
{ id: 3, type: 'loan' },
{ id: 4, type: 'credit', currency: 'RUB' },
{ id: 5, type: 'credit', currency: 'RUB' },
{ id: 6, type: 'debit', currency: 'USD' },
{ id: 7, type: 'debit', currency: 'RUB' },
{ id: 8, type: 'external' },
{ id: 9, type: 'credit', currency: 'EUR' }
];
accounts.sort((a, b) => {
});
uj5u.com熱心網友回復:
您可以將type和currency屬性的順序存盤在陣列中,然后減去索引以確定優先級:
const typeOrder = ['debit', 'credit', 'external', 'loan']
const currencyOrder = ['RUB', 'USD', 'EUR', 'GBT']
const accounts=[{id:1,type:"credit",currency:"GBP"},{id:2,type:"debit",currency:"EUR"},{id:3,type:"loan"},{id:4,type:"credit",currency:"RUB"},{id:5,type:"credit",currency:"RUB"},{id:6,type:"debit",currency:"USD"},{id:7,type:"debit",currency:"RUB"},{id:8,type:"external"},{id:9,type:"credit",currency:"EUR"}];accounts.sort((e,c)=>{});
const res = accounts.sort((a,b) => {
return typeOrder.indexOf(a.type) - typeOrder.indexOf(b.type) || currencyOrder.indexOf(a.currency) - currencyOrder.indexOf(b.currency)
})
console.log(res)
uj5u.com熱心網友回復:
您可以為訂單獲取物件并使用邏輯 OR 鏈接增量。
const
types = { debit: 1, credit: 2, external: 3, loan: 4 },
currencies = { RUB: 1, USD: 2, EUR: 3, GBT: 4 },
accounts = [{ id: 1, type: 'credit', currency: 'GBP' }, { id: 2, type: 'debit', currency: 'EUR' }, { id: 3, type: 'loan' }, { id: 4, type: 'credit', currency: 'RUB' }, { id: 5, type: 'credit', currency: 'RUB' }, { id: 6, type: 'debit', currency: 'USD' }, { id: 7, type: 'debit', currency: 'RUB' }, { id: 8, type: 'external' }, { id: 9,
type: 'credit', currency: 'EUR' }];
accounts.sort((a, b) =>
types[a.type] - types[b.type] ||
currencies[a.currency] - currencies[b.currency]
);
console.log(accounts);
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
這是最簡單的
當 order1 為 0(a 和 b 相同)時,我們使用 order2
const order1 = ["debit","credit","external","loan"],
order2 = ["RUB","USD","EUR","GBT"]
const accounts = [ { id: 1, type: 'credit', currency: 'GBP', }, { id: 2, type: 'debit', currency: 'EUR', }, { id: 3, type: 'loan', }, { id: 4, type: 'credit', currency: 'RUB', }, { id: 5, type: 'credit', currency: 'RUB', }, { id: 6, type: 'debit', currency: 'USD', }, { id: 7, type: 'debit', currency: 'RUB', }, { id: 8, type: 'external', }, { id: 9, type: 'credit', currency: 'EUR', }, ];
accounts.sort((a, b) => {
return order1.indexOf(a.type) - order1.indexOf(b.type) ||
order2.indexOf(a.currency) - order2.indexOf(b.currency)
});
console.log(accounts)
uj5u.com熱心網友回復:
您可以創建兩個映射來存盤每個類別的“優先級”,排序結果將是型別優先級之間的差異,如果為 0,則為貨幣的差異:
const accounts = [ { id: 1, type: 'credit', currency: 'GBP' }, { id: 2, type: 'debit', currency: 'EUR' }, { id: 3, type: 'loan' }, { id: 4, type: 'credit', currency: 'RUB' }, { id: 5, type: 'credit', currency: 'RUB' }, { id: 6, type: 'debit', currency: 'USD' }, { id: 7, type: 'debit', currency: 'RUB' }, { id: 8, type: 'external' }, { id: 9, type: 'credit', currency: 'EUR' } ];
const
TYPE_PRIORITY = { 'debit': 1, 'credit': 2, 'external': 3, 'loan': 4 },
CURRENCY_PRIORITY = { 'RUB': 1, 'USD': 2, 'EUR': 3, 'GBP': 4 };
accounts.sort((a, b) =>
TYPE_PRIORITY[a.type] - TYPE_PRIORITY[b.type] ||
CURRENCY_PRIORITY[a.currency] - CURRENCY_PRIORITY[b.currency]
);
console.log(accounts);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/352346.html
標籤:javascript
下一篇:PHP從按鈕獲取值而不提交
