JavaScript 数组对象排序:根据 preId 和 nextId 重新排列数组
JavaScript 数组对象排序:根据 preId 和 nextId 重新排列数组
现有数组对象如下:
const arr = [
{
id: 1,
list: [
{
preId: '',
nextId: '',
id: 'N1',
},
],
},
{
id: 2,
list: [
{
preId: 'N4',
nextId: '',
id: 'N2',
},
],
},
{
id: 3,
list: [
{
preId: '',
nextId: 'N6',
id: 'N3',
},
],
},
{
id: 4,
list: [
{
preId: 'N6',
nextId: 'N2',
id: 'N4',
},
],
},
{
id: 5,
list: [
{
preId: '',
nextId: '',
id: 'N5',
},
],
},
{
id: 6,
list: [
{
preId: 'N3',
nextId: 'N4',
id: 'N6',
},
],
},
];
条件:
- list 中的对象中:
preId代表排序在该对象 前面 的对象的 id;nextId: 代表排序在该对象 后面 的对象的 id;
- 原数组中,每个 list 中只会有一个对象;
目的:
现在需要重新筛选排列该数组,根据每个对象中 list 的对象将有前后关系的对象归类,造出一个新数组对象;list 中对象是拥有先后顺序,且 list 中的用 outerId 来记录属于原来外层的 id。
处理完成后的数组如下:
[
{
id: 1,
list: [
{
preId: '',
nextId: '',
id: 'N1',
outerId: 1,
},
],
},
{
id: 3,
list: [
{
preId: '',
nextId: 'N6',
id: 'N3',
outerId: 3,
},
{
preId: 'N3',
nextId: 'N4',
id: 'N6',
outerId: 6,
},
{
preId: 'N6',
nextId: 'N2',
id: 'N4',
outerId: 4,
},
{
preId: 'N4',
nextId: '',
id: 'N2',
outerId: 2,
},
],
},
{
id: 5,
list: [
{
preId: '',
nextId: '',
id: 'N5',
outerId: 5,
},
],
},
];
实现思路:
- 遍历原数组,将每个
list中的对象根据preId和nextId进行分类,分别存储在两个数组中。 - 对存储
preId的数组进行排序,按照preId的值从小到大排序。 - 对每个存储
preId的数组中的对象,找到其对应的nextId,将其添加到该对象的list中。 - 将存储
preId和存储nextId的两个数组合并成一个数组,并按照id的值从小到大排序。 - 遍历排序后的数组,根据每个对象的
preId和nextId将其添加到对应的对象的list中,并记录其outerId。 - 最终得到处理完成后的数组。
代码实现如下:
const arr = [ /* 原数组 */ ];
// 分类存储preId和nextId的对象
let preList = [];
let nextList = [];
arr.forEach(item => {
const { id, list } = item;
list.forEach(obj => {
if (obj.preId !== '') {
preList.push({
id,
outerId: id,
...obj
});
}
if (obj.nextId !== '') {
nextList.push({
id,
outerId: id,
...obj
});
}
});
});
// 对存储preId的数组进行排序
preList.sort((a, b) => {
if (a.preId === b.id) {
return -1;
}
if (a.preId === b.preId) {
return 0;
}
return a.preId < b.preId ? -1 : 1;
});
// 将每个对象的nextId添加到其对应的list中
preList.forEach(preObj => {
const nextObj = nextList.find(nextObj => nextObj.id === preObj.id && nextObj.preId === preObj.nextId);
if (nextObj) {
preObj.nextObj = nextObj;
}
});
// 合并存储preId和存储nextId的数组,并按照id排序
const sortedList = [...preList, ...nextList].sort((a, b) => a.id - b.id || a.outerId - b.outerId);
// 根据preId和nextId将对象添加到对应的对象的list中,并记录outerId
const result = [];
sortedList.forEach(obj => {
const { id, outerId, preId, nextId } = obj;
const item = result.find(item => item.id === id);
if (!item) {
result.push({
id,
list: [{
id: obj.id,
preId: obj.preId,
nextId: obj.nextId,
outerId: obj.outerId
}]
});
} else {
const index = item.list.findIndex(item => item.id === preId);
if (index === -1) {
if (nextId === '') {
item.list.push({
id: obj.id,
preId: obj.preId,
nextId: obj.nextId,
outerId: obj.outerId
});
} else {
item.list.unshift({
id: obj.id,
preId: obj.preId,
nextId: obj.nextId,
outerId: obj.outerId
});
}
} else {
if (nextId === '') {
item.list.splice(index + 1, 0, {
id: obj.id,
preId: obj.preId,
nextId: obj.nextId,
outerId: obj.outerId
});
} else {
item.list.splice(index, 0, {
id: obj.id,
preId: obj.preId,
nextId: obj.nextId,
outerId: obj.outerId
});
}
}
}
});
console.log(result);
总结:
本文提供了一种根据 preId 和 nextId 属性重新排列数组对象的解决方案,并通过代码示例展示了具体的实现步骤。该方法能够有效地将有前后关系的对象归类到同一个数组对象中,并记录每个对象的 outerId,方便后续操作。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/otMX 著作权归作者所有。请勿转载和采集!