给定一个像这样的对象:
accounts = [
{ bankType: "Checking", currency: "USD", amount: 123.45 },
{ bankType: "Saving", currency: "CAD", amount: 1.95 },
{ bankType: "Saving", currency: "USD", amount: 23.31 },
{ bankType: "Checking", currency: "CAD", amount: 1953.1 },
];
如何按数组中的对象属性进行排序,其中bankType先"Checkings"排序,然后再排序currency帐户"CAD",以获得下面的结果?
// Sorted array of objects result
[
{ bankType: "Checking", currency: "CAD", amount: 1953.1 },
{ bankType: "Checking", currency: "USD", amount: 123.45 },
{ bankType: "Saving", currency: "CAD", amount: 1.95 },
{ bankType: "Saving", currency: "USD", amount: 23.31 },
];
问题不在于使用内置localeCompare函数按字母顺序对其进行排序,问题在于必须按Checking第一个然后按CAD第二个的特定常量值进行排序。
holdtom
慕容708150
慕无忌1623718
手掌心
随时随地看视频慕课网APP
相关分类