我目前有一個configuration被映射的物件 ( ),以便在總結螢屏上顯示每個專案。但是,我需要在地圖之前對物件進行排序,但無法訪問我需要的特定值。
const ConfigComponentSummaryContainer = ({ configuration }) => (
<Grid container spacing={4}>
{Object.keys(configuration)
.map(key => {
const config = configuration[key];
if (config && config.length && !restrictList.includes(config[0].component.grouping.toLowerCase())) {
return config
.map(c => (
<Grid item xs={6} ref={componentRef}>
<ConfigComponentSummary configuration={c} />
</Grid>
));
}
return null;
})}
</Grid>
);
這是配置物件在控制臺中的樣子:

我需要seq在組件內部獲取一個值 ( ) 以用于排序。如何在映射鍵之前訪問該特定值和排序?
uj5u.com熱心網友回復:
使用排序。您可以像在 map 中一樣使用鍵并查找物件,或者使用獲取鍵和值的條目。
Object.entries(configuration)
.sort((a, b) => a[1][0].component.seq - b[1][0].component.seq)
.map(([key, config]) => {});
uj5u.com熱心網友回復:
只有一種資料結構可以用于排序,它是 Array。我想您需要將物件轉換為按seq組件排序的陣列,然后將其用于映射
uj5u.com熱心網友回復:
您可以使用lodash。它有一個sortBy你應該能夠像這樣使用的方法:
_.sortBy(configuration, "component.seq")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/405595.html
標籤:
