JavaScript 数组对象排序与归类:根据 preId 和 nextId 重新排列数组

现有数组对象如下:

[
  {
    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,
      },
    ],
  },
];

思路

  1. 遍历原数组,将每个 list 中的对象按照 preId 和 nextId 关系进行归类,存放到一个新的对象中。
  2. 对于每个归类后的对象,按照归类的顺序重新排列 list 中的对象。
  3. 将重新排列后的对象放入新的数组中,生成处理完成后的数组。

代码实现

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',
      },
    ],
  },
];

const newObj = {}; // 存放归类后的对象

// 遍历原数组,将每个list中的对象按照preId和nextId关系进行归类
arr.forEach((item) => {
  item.list.forEach((obj) => {
    if (!newObj[obj.id]) {
      newObj[obj.id] = {
        id: obj.id,
        preId: obj.preId,
        nextId: obj.nextId,
        outerId: item.id,
        list: [],
      };
    } else {
      newObj[obj.id].outerId = item.id;
      newObj[obj.id].preId = obj.preId;
      newObj[obj.id].nextId = obj.nextId;
    }
  });
});

// 对于每个归类后的对象,按照归类的顺序重新排列list中的对象
Object.keys(newObj).forEach((key) => {
  const obj = newObj[key];
  if (obj.preId) {
    newObj[obj.preId].list.push(obj);
  } else {
    newObj[`start_${obj.outerId}`] = obj;
  }
});

// 将重新排列后的对象放入新的数组中,生成处理完成后的数组
const newArr = [];
Object.keys(newObj).forEach((key) => {
  if (!key.startsWith('start_')) {
    return;
  }
  const obj = newObj[key];
  delete obj.preId;
  delete obj.nextId;
  delete obj.outerId;
  obj.list.sort((a, b) => {
    if (a.preId === b.id) {
      return -1;
    }
    if (b.preId === a.id) {
      return 1;
    }
    return 0;
  });
  newArr.push(obj);
});

console.log(newArr); // 输出处理完成后的数组

代码解析:

  1. 创建 newObj 对象: 用于存放归类后的对象,key 为 id,value 为一个包含 id、preId、nextId、outerId 和 list 属性的对象。
  2. 遍历原数组: 使用 forEach 方法遍历原数组,并对每个 list 对象进行处理。
  3. 归类对象: 使用 if 语句判断 newObj 中是否已经存在该对象,如果不存在则创建一个新的对象并存储到 newObj 中,否则更新该对象的 outerId、preId 和 nextId 属性。
  4. 重新排列对象: 使用 forEach 方法遍历 newObj 对象,并将每个对象按照 preId 和 nextId 关系进行排序,最终将排序后的对象放入 newArr 数组中。
  5. 生成最终数组: 遍历 newArr 数组,删除每个对象中的 preId、nextId 和 outerId 属性,并按照 preId 和 nextId 关系对 list 进行排序。

注意:

  • 由于需要根据 preId 和 nextId 关系进行排序,因此需要使用 sort 方法对 list 进行排序。
  • 代码中使用 start_ 作为 key 的前缀,目的是为了标识 list 中的第一个对象,以便后续遍历时可以从第一个对象开始。

希望本篇文章能够帮助你理解如何使用 JavaScript 对数组对象进行排序和归类。

JavaScript 数组对象排序与归类:根据 preId 和 nextId 重新排列数组