JavaScript数组对象排序和归类:根据前后关系重新排列数组

现有数组对象如下:

[
  {
    "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 按照前后关系进行排序,然后根据排序后的结果,按照前后关系将对象进行归类并生成新的数组。

具体实现:

  1. 遍历原始数组,将每个对象中的 list 按照前后关系进行排序。
  2. 建立一个空对象,用于存储归类后的对象。
  3. 遍历排序后的数组,根据前后关系将对象归类,并将归类后的对象存储到空对象中。
  4. 将空对象中的对象按照 id 排序,并生成新的数组对象。

代码实现:

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按照前后关系进行排序
const sortedArr = arr.map(item => {
  const list = item.list.sort((a, b) => {
    if (a.preId === b.id) {
      return -1;
    } else if (a.nextId === b.id) {
      return 1;
    } else {
      return 0;
    }
  });
  return {
    ...item,
    list
  };
});

// 建立一个空对象,用于存储归类后的对象
const groupedObj = {};

// 遍历排序后的数组,根据前后关系将对象归类
sortedArr.forEach(item => {
  let temp = groupedObj;
  item.list.forEach(listItem => {
    if (!temp[listItem.id]) {
      temp[listItem.id] = { id: listItem.id };
    }
    temp = temp[listItem.id];
  });
  temp[item.id] = { ...item, outerId: item.id };
});

// 将空对象中的对象按照id排序,并生成新的数组对象
const resultArr = Object.values(groupedObj).sort((a, b) => a.id - b.id);

console.log(resultArr);

代码解释:

  • 排序: 使用 map 方法遍历原始数组,并使用 sort 方法对每个对象中的 list 进行排序,根据 preIdnextId 属性进行比较,确定排序顺序。
  • 归类: 使用一个空对象 groupedObj 来存储归类后的对象,遍历排序后的数组,使用嵌套循环,将每个 listItemid 作为 groupedObj 对象的属性,并依次添加其关联对象。
  • 生成新数组: 使用 Object.values 方法获取 groupedObj 对象的所有值,并使用 sort 方法对这些值按照 id 属性进行排序,最后生成新的数组对象。

希望本文能够帮助您理解JavaScript数组对象排序和归类的操作方法,并提供一些可参考的代码实现思路。

JavaScript数组对象排序和归类:根据前后关系重新排列数组

原文地址: https://www.cveoy.top/t/topic/otD0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录