【发布时间】:2019-04-02 04:14:35
【问题描述】:
我正在尝试重新排列数组的排序。 假设我有以下结构
const array = [{
location: 'Table 2',
data: {..}
}, {
location: 'Unassigned',
data: {..}
}, {
location: 'Table 1',
data: {..}
}
];
将“表 1”移动到索引 0、“表 2”紧随其后(表 3、4 等保持相同顺序)以及“未分配”始终移动到末尾的正确方法是什么。最好使用 lodash。
这是我目前尝试过的
forEach(allItemsSorted, (item, index) => {
const total = allItemsSorted.length;
let hasUnassigned = false;
if (item.location === 'Unassigned') {
allItemsSorted[total] = item;
hasUnassigned = true;
}
if (hasUnassigned && index === total) {
return;
}
allItemsSorted[index] = item;
})
【问题讨论】:
-
为什么不使用
sort
标签: javascript ecmascript-6 lodash