我有一個像下面這樣的物件陣列:
var stores = [
{
"name": "store3",
"ditance": 8
},
{
"name": "Store5",
"distance": 7,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isFutureOrderPossible": false
}
}
},
{
"name": "Store1",
"distance": 12,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isOpen": true
}
}
},
{
"name": "store2",
"distance": 13,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isOpen": true
}
}
}
]
根據 isopen 和距離排序的預期結果
[
{
"name": "Store1",
"distance": 12,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isOpen": true
}
}
},
{
"name": "store2",
"distance": 13,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isOpen": true
}
}
},
{
"name": "Store5",
"distance": 7,
"web": {
"validateAttributes": {
"isSelectedDispostionSupported": true,
"isFutureOrderPossible": false
}
}
},
{
"name": "store3",
"ditance": 8
}
]
需要根據 isOpen 和 distance 對陣列進行排序。挑戰是某些物件具有 web 屬性而某些物件沒有。即使有時可用的網路 isOpen 也不會。我試過下面的方法它不起作用
const sorter = (a, b) => {
if (a.web) {
if (a.web.validateAttributes) {
if (a.web.validateAttributes.isOpen) {
return 1;
} else if (b.web.validateAttributes.isOpen) {
return -1;
} else {
return 1;
};
} else {
return 1;
}
} else {
return 1;
}
};
stores.sort(sorter);
uj5u.com熱心網友回復:
您可以使用一些可選的鏈接將isOpen屬性轉換為 -1(如果為真)或 1(如果為假)以建立它們的位置。
如果這些屬性相等,則回退到距離比較。
// fixed "ditance" to "distance" in "store3"
const stores = [{"name":"store3","distance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}]
const sorted = [...stores].sort((a, b) =>
(a.web?.validateAttributes?.isOpen ? -1 : 1) -
(b.web?.validateAttributes?.isOpen ? -1 : 1) ||
(a.distance - b.distance))
console.log(sorted)
.as-console-wrapper { max-height: 100% !important; }
uj5u.com熱心網友回復:
我看到 2 個問題。
- 第一個專案“距離”的錯字:8
- 你的排序功能。這是我的邏輯。首先,檢查兩者是打開還是關閉,然后比較距離。否則只是簡單比較狀態打開/關閉
var stores = [{"name":"store3","distance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}];
stores.sort((a, b) => {
if (a.web?.validateAttributes?.isOpen && b.web?.validateAttributes?.isOpen || !a.web?.validateAttributes?.isOpen && !b.web?.validateAttributes?.isOpen) {
return a.distance - b.distance;
}
if (a.web?.validateAttributes?.isOpen) {
return -1;
}
return 1;
})
console.log(stores);
uj5u.com熱心網友回復:
嘗試這個。
const stores = [{"name":"store3","ditance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}]
const sorted = stores.slice();
sorted.sort((a, b) => {
if (!a.web || !b.web) {
return -1;
}
if (!a.web.validateAttributes || !b.web.validateAttributes) {
return -1;
}
const x = a.web.validateAttributes.isOpen || false;
const y = b.web.validateAttributes.isOpen || false;
return (x === y) ? a.distance - b.distance : -1;
})
console.log(sorted)
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
嘗試這個
const stores = [{"name":"store3","ditance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}]
const sorter = (a,b)=> {
if(!b.web) {
return -1;
}
else if(!b.web.validateAttributes) {
return -1;
}
else if(!b.web.validateAttributes.isOpen) {
return -1;
}
return (a.distance - b.distance)
}
console.log(stores.sort(sorter))
uj5u.com熱心網友回復:
在修復了一些錯別字之后,這里是另一種選擇。
如果不設定,默認每isOpen到false和存盤。如果它們相同,則按 排序distance,否則按 排序isOpen。
這使用了Optional Chaining 運算子和Nullish Coalescing 運算子的組合
null如果鏈上的任何屬性不存在,Optional Chaining 運算子將回傳。所以在存在.web或.isOpen不存在的情況下null產生一個。
然后是 Nullish Caolescing 運算子,因此如果 anull如上所述生成,則默認值是false這樣的排序順序基于該值(true將放在false.
let stores = [{"name":"Store1","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"Store5","distance":8,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"store3","distance":7}];
function comparator(a,b) {
let aResult = a?.web?.validateAttributes?.isOpen ?? false;
let bResult = b?.web?.validateAttributes?.isOpen ?? false;
return (aResult === bResult)? a.distance - b.distance : bResult - aResult;
}
console.log(stores.sort(comparator));
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/377981.html
標籤:javascript 数组 有角的 排序 过滤
上一篇:如何從模式匹配中獲取索引?
