[英]Sort array of objects in JavaScript based on object keys substring
我有一系列對象
[
{
Severity: "<span class='tableActive'></span>",
Name: 'U3B',
'U1A_Shift SCM: UPTT-Pressure (Bara)': '-',
'U1A_Shift SCM: DPTT-Pressure (Bara)': '-',
'U3B SCM: APTT-Pressure (Bara)': '3510.00',
'U3B SCM: UPTT-Pressure (Bara)': '3413.00',
'U1B SCM: DPTT-Pressure (Bara)': '-',
'U1B SCM: UPTT-Pressure (Bara)': '-',
'U3B SCM: DPTT-Pressure (Bara)': '740.00',
'U1A_Shift SCM: UPTT-Temp (DegC)': '-',
'U1A_Shift SCM: DPTT-Temp (DegC)': '-',
'U3B SCM: APTT-Temp (DegC)': '1565.00',
'U3B SCM: UPTT-Temp (DegC)': '2654.00',
'U1B SCM: DPTT-Temp (DegC)': '-',
'U1B SCM: UPTT-Temp (DegC)': '-',
'U3B SCM: DPTT-Temp (DegC)': '3159.00',
'U1B SCM: PCV-CHOKE status - Control position': '-',
'U3B SCM: PCV-CHOKE status - Control position': '-',
'U1A_Shift SCM: PCV-CHOKE status - Control position': '-',
Alarms: 0,
Advisories: 0,
__row_index: 0,
},
]
因此 output 應該是
[
{
Severity: "<span class='tableActive'></span>",
Name: 'U3B',
'U1A_Shift SCM: UPTT-Pressure (Bara)': '-', // grouped by UPTT-Pressure (Bara)
'U3B SCM: UPTT-Pressure (Bara)': '3413.00',
'U1B SCM: UPTT-Pressure (Bara)': '-',
'U1A_Shift SCM: DPTT-Pressure (Bara)': '-', //grouped by DPTT-Pressure (Bara)
'U1B SCM: DPTT-Pressure (Bara)': '-',
'U3B SCM: DPTT-Pressure (Bara)': '740.00',
'U3B SCM: APTT-Pressure (Bara)': '3510.00', // grouped by APTT-Pressure (Bara)
'U1A_Shift SCM: UPTT-Temp (DegC)': '-', // grouped by UPTT-Temp (DegC)
'U3B SCM: UPTT-Temp (DegC)': '2654.00',
'U1B SCM: UPTT-Temp (DegC)': '-',
'U1A_Shift SCM: DPTT-Temp (DegC)': '-', // grouped by DPTT-Temp (DegC)
'U1B SCM: DPTT-Temp (DegC)': '-',
'U3B SCM: DPTT-Temp (DegC)': '3159.00',
'U3B SCM: APTT-Temp (DegC)': '1565.00', // grouped by APTT-Temp (DegC)
'U1B SCM: PCV-CHOKE status - Control position': '-', // grouped by PCV-CHOKE status - Control position
'U3B SCM: PCV-CHOKE status - Control position': '-',
'U1A_Shift SCM: PCV-CHOKE status - Control position': '-',
Alarms: 0,
Advisories: 0,
__row_index: 0,
}
]
我需要根據“:”之后的鍵名對這個對象數組進行排序,例如:APTT-Temp (DegC)
我只列出一個 object 我收到了對象數組
我需要對這個對象數組進行排序,基本上按“:”(冒號)之后的鍵值分組
您只需稍微修改以下 SO 問題的答案,以便它比較(例如整數)值而不是鍵Sorting a array of objects by object key 。 或者,您可以使用此 SO 問題的解決方案: 如何排序 function 在 JavaScript 中工作,以及比較 function
工作示例:
var arr = [ { Severity: "<span class='tableActive'></span>", Name: 'U3B', 'U3B SCM: UPTT-Pressure (Bara)': '3413.00', 'U3B SCM: APTT-Temp (DegC)': '1565.00', 'U3B SCM: PCV-CHOKE status - Control position': '-', 'U1A_Shift SCM: PCV-CHOKE status - Control position': '-', Alarms: 0, Advisories: 0, __row_index: 0 }, { Severity: "<span class='tableActive'></span>", Name: 'U3B', 'U3B SCM: UPTT-Pressure (Bara)': '3413.00', 'U3B SCM: APTT-Temp (DegC)': '840.00', 'U3B SCM: PCV-CHOKE status - Control position': '-', 'U1A_Shift SCM: PCV-CHOKE status - Control position': '-', Alarms: 0, Advisories: 0, __row_index: 0 }, { Severity: "<span class='tableActive'></span>", Name: 'U3B', 'U3B SCM: UPTT-Pressure (Bara)': '3413.00', 'U3B SCM: APTT-Temp (DegC)': '1427.00', 'U3B SCM: PCV-CHOKE status - Control position': '-', 'U1A_Shift SCM: PCV-CHOKE status - Control position': '-', Alarms: 0, Advisories: 0, __row_index: 0 } ]; arr.sort( function(a, b) { const aValue = parseInt(a['U3B SCM: APTT-Temp (DegC)']); const bValue = parseInt(b['U3B SCM: APTT-Temp (DegC)']); return aValue - bValue; }); console.log(arr)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.