javascript - 在 JavaScript 中按属性对整个嵌套对象进行排序

标签 javascript arrays object

我想遍历一个深度嵌套的对象,并根据属性对每个级别进行排序。在这种情况下,它的 id

这是我的对象(我还会有更多级别,为了便于阅读,我在这里添加了 3 个级别):

const myObj = [
  {
    id: 15,
    children: [
      {
        id: 9,
        children: [
          {
            id: 4,
            children: []
          },
          {
            id: 1,
            children: []
          }
        ]
      },
      {
        id: 4,
        children: [
          {
            id: 35,
            children: [
              {
                id: 12,
                children: []
              },
              {
                id: 8,
                children: []
              }
            ]
          },
          {
            id: 30,
            children: [],
          }
        ]
      },
    ]
  },
  {
    id: 2,
    children: [
      {
        id: 9,
        children: []
      },
      {
        id: 3,
        children: []
      },
    ]
  }
]

这是期望的输出:

const myObj = [
  {
    id: 2,
    children: [
      {
        id: 3,
        children: []
      },
      {
        id: 9,
        children: []
      }
    ]
  },
  {
    id: 15,
    children: [
      {
        id: 4,
        children: [
          {
            id: 30,
            children: [],
          },
          {
            id: 35,
            children: [
              {
                id: 8,
                children: []
              },
              {
                id: 12,
                children: []
              }
            ]
          },
        ]
      },
      {
        id: 9,
        children: [
          {
            id: 1,
            children: []
          },
          {
            id: 4,
            children: []
          }
        ]
      },
    ]
  }
]

这是我对其进行排序的尝试:

const myObj = [{id:15,children:[{id:9,children:[{id:4,children:[]},{id:1,children:[]}]},{id:4,children:[{id:35,children:[{id:12,children:[]},{id:8,children:[]}]},{id:30,children:[],}]},]},{id:2,children:[{id:9,children:[]},{id:3,children:[]},]}]

function sortByOrderIndex(obj) {
  obj.sort((a, b) => (a.orderindex > b.orderindex) ? 1 : ((b.orderindex > a.orderindex) ? -1 : 0));

  return obj;
}

function sortNestedObj(obj) {
  sortByOrderIndex(obj);

  for (let i = 0; i < obj.length; i++) {
    const t = obj[i];

    if (t.children.length !== 0) {
      sortNestedObj(t.children);
    } else {
      return;
    }
  }
}

console.log(sortByOrderIndex(myObj))

我创建了一个对对象进行排序的函数,然后尝试创建另一个对象,循环遍历每个有子对象的对象,并使用第一个函数对这些子对象进行排序。如果这些 child 有 child ,则对这些 child 进行排序,依此类推,直到一个 child 没有 child 。

最佳答案

你可以递归地sort数组及其对象的 children 如下所示:

const myObj = [{id:15,children:[{id:9,children:[{id:4,children:[]},{id:1,children:[]}]},{id:4,children:[{id:35,children:[{id:12,children:[]},{id:8,children:[]}]},{id:30,children:[],}]},]},{id:2,children:[{id:9,children:[]},{id:3,children:[]},]}]

function sortArray(array) {
  array.sort((a, b) => a.id - b.id);
  array.forEach(a => {
    if (a.children && a.children.length > 0)
      sortArray(a.children)
  })
  return array;
}

console.log(sortArray(myObj))

关于javascript - 在 JavaScript 中按属性对整个嵌套对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55235063/

相关文章:

javascript - 从父 Controller AngularjS 访问 Popup 控件

javascript - Nativescript 使用中继器多键值

javascript - 将每个数组值与 ajax 传递的数组分开——php

python - 计算数组python中的事件数

javascript - 当我排除某种类型时, typescript 永远不会显示

javascript - JS 对象事件

javascript - 无效的日期/时间字符串 : Sun Jun 03 2012 00:00:00 GMT+0100 (GMT Daylight Time) CFML

javascript - 如何将输入字段扩展到 div 末尾

c - 修复 C 代码和参数修复编译错误

object - 如何避免 Rebol 中的对象函数和全局函数之间的名称冲突?