JS 对对象数组进行排序

Til*_*ode 5 javascript arrays sorting

大家好,我仍然无法正确对数组进行排序。我们需要对以下数组进行排序:

const arr = [
     {id: 3123124, parentId: 0o0000, title: 'title', type: 'block'},
     {id: 3542132, parentId: 3123124, title: 'title', type: 'child'},
     {id: 12345, parentId: 88888, title: 'title', type: 'block'},
     {id: 24124, parentId: 12345, title: 'title', type: 'child'},
     {id: 99999, parentId: 45324, title: 'title', type: 'child'},
     {id: 986648, parentId: 3123124, title: 'title', type: 'block'},
     {id: 77777, parentId: 88888, title: 'title', type: 'child'},
     {id: 54232, parentId: 3123124, title: 'title', type: 'child'},
     {id: 54308, parentId: 15075, title: 'title', type: 'child'},
     {id: 66666, parentId: 88888, title: 'title', type: 'block'},
     {id: 56445, parentId: 12345, title: 'title', type: 'child'},
     {id: 88888, parentId: 45324, title: 'title', type: 'block'},
     {id: 15075, parentId: 12345, title: 'title', type: 'block'},
     {id: 84356, parentId: 66666, title: 'title', type: 'child'},
     {id: 45324, parentId: 0o0000, title: 'title', type: 'block'},
]

const newArr = [
    {id: 3123124, parentId: 0o0000, title: 'title', type: 'block'},
    {id: 3542132, parentId: 3123124, title: 'title', type: 'child'},
    {id: 54232, parentId: 3123124, title: 'title', type: 'child'},
    {id: 986648, parentId: 3123124, title: 'title', type: 'block'},
    {id: 45324, parentId: 0o0000, title: 'title', type: 'block'},
    {id: 99999, parentId: 45324, title: 'title', type: 'child'},
    {id: 88888, parentId: 45324, title: 'title', type: 'block'},
    {id: 77777, parentId: 88888, title: 'title', type: 'child'},
    {id: 12345, parentId: 88888, title: 'title', type: 'block'},
    {id: 56445, parentId: 12345, title: 'title', type: 'child'},
    {id: 24124, parentId: 12345, title: 'title', type: 'child'},
    {id: 15075, parentId: 12345, title: 'title', type: 'block'},
    {id: 54308, parentId: 15075, title: 'title', type: 'child'},
    {id: 66666, parentId: 88888, title: 'title', type: 'block'},
    {id: 84356, parentId: 66666, title: 'title', type: 'child'},
]
Run Code Online (Sandbox Code Playgroud)
  • arr - 要排序的数组
  • newArr - 是一个数组,应该是

因此,最顶层的元素具有parentId: 0o000,而它们的最低子元素具有相同的parentId id,依此类推,可以有任意多个元素。如果parentId多个元素相同,则child排在最前面,最新的block及其子元素在下面(如果有的话)。

我尝试通过属性添加到新数组中parentId,但最终元素的顺序丢失了

Nin*_*olz 5

您可以将一个对象作为 的查找,id并使用另一个对象来按父级分组,然后首先按父级的顺序获取所有项目,并block在每个级别按第一个排序。

const
    getItems = parent => (parents[parent] || [])
        .sort((a, b) => (ids[a].type === 'block') - (ids[b].type === 'block'))
        .flatMap(id => [ids[id], ...getItems(id)]),
    data = [{ id: 3123124, parentId: 0o0000, title: 'title', type: 'block' }, { id: 3542132, parentId: 3123124, title: 'title', type: 'child' }, { id: 12345, parentId: 88888, title: 'title', type: 'block' }, { id: 24124, parentId: 12345, title: 'title', type: 'child' }, { id: 99999, parentId: 45324, title: 'title', type: 'child' }, { id: 986648, parentId: 3123124, title: 'title', type: 'block' }, { id: 77777, parentId: 88888, title: 'title', type: 'child' }, { id: 54232, parentId: 3123124, title: 'title', type: 'child' }, { id: 54308, parentId: 15075, title: 'title', type: 'child' }, { id: 66666, parentId: 88888, title: 'title', type: 'block' }, { id: 56445, parentId: 12345, title: 'title', type: 'child' }, { id: 88888, parentId: 45324, title: 'title', type: 'block' }, { id: 15075, parentId: 12345, title: 'title', type: 'block' }, { id: 84356, parentId: 66666, title: 'title', type: 'child' }, { id: 45324, parentId: 0o0000, title: 'title', type: 'block' }],
    parents = data.reduce((r, { id, parentId }) => ((r[parentId] ??= []).push(id), r), {}),
    ids = Object.fromEntries(data.map(o => [o.id, o])),
    sorted = getItems(0);

console.log(sorted);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)