【发布时间】:2022-01-13 23:31:34
【问题描述】:
我有一个对象数组,如下所示:
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 对数组进行排序。挑战是一些对象有网络属性,而一些对象没有。即使网络有时可用 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);
【问题讨论】:
-
预期的结果应该是什么?请自行添加问题...
-
@decpk 提供
-
你真的只有一个条目有
ditance属性吗? -
@Phil 距离也是他们的。需要先根据isOpen和距离排序
标签: javascript arrays angular sorting filtering