最有效的方式是通过另一个深度嵌套的对象数组对一个深度嵌套的对象数组进行排序 [英] Most performant way to sort a deeply nested array of objects by another deeply nested array of objects

查看:53
本文介绍了最有效的方式是通过另一个深度嵌套的对象数组对一个深度嵌套的对象数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为示例-我包含一个元素 array ,其中包含一个 object ,该对象具有一个 Children 键,该键是一个对象数组,每个对象都有自己的 Children 键,其中包含另一个数组。

As an example - I've included a one element array that contains an object that has a Children key, which is an array of objects and each object also has its' own Children key that contains another array.

[
  {
    "Id": "1",
    "Children": [
      {
        "Id": "2",
        "Children": [
          {
            "Id": "10",
            "DisplayName": "3-4",
          },
          {
            "Id": "1000",
            "DisplayName": "5-6",
          },
          {
            "Id": "100",
            "DisplayName": "1-2",
          },
        ]
      }
    ]
  }
]

我想第二个对象数组 strong>将第一个对象数组与意图进行比较确保确保第一个数组与对象的第二个数组处于相同的顺序,如果不是,则 >排序,直到完成。

There is a second array of objects that I would like to compare the first array of objects to, with the intention of making sure that the first array is in the same order as the second array of objects, and if it is not - then sort until it is.

这是第二个数组:

[
  {
    "Id": "1",
    "Children": [
      {
        "Id": "2",
        "Children": [
           {
            "Id": "100",
            "DisplayName": "1-2",
          },
          {
            "Id": "10",
            "DisplayName": "3-4",
          },
          {
            "Id": "1000",
            "DisplayName": "5-6",
          },
        ]
      }
    ]
  }
]

将要运行的数据可能高达数万个,因此性能至关重要。

The data that this will run on can be up in the tens of thousands - so performance is paramount.

我目前正在尝试使用实用程序方法将第二个数组的每个元素转换为对象的键控对象,例如

What I'm currently attempting is using a utility method to convert each element of the second array into a keyed object of objects e.g.

{
   1:  {
        "Id": "1",
        "Children": [
          {
            "Id": "2",
            "Children": [
              {
                "Id": "4",
                "DisplayName": "3-4",
              },
              {
                "Id": "3",
                "DisplayName": "1-2",
              },
            ]
          }
        ]
      }
}

从顶层快速查找。我想知道我是否应该一直继续这样做,或者是否有惯用的方式来完成此任务。我也考虑了递归。
已排序数组的顺序不是基于ID的-它是任意的。因此,无论如何都需要保留订单。

This allows fast look up from the top level. I'm wondering if I should continue doing this all the way down or if there is an idiomatic way to accomplish this. I considered recursion as well. The order of the already sorted array is not based on Id - it is arbitrary. So the order needs to be preserved regardless.

推荐答案

假设每个对象的每个级别中的深度相同且所有ID存在,则使用递归函数,该递归函数使用 Array#findIndex() 排序回调

Assuming same depth and all Id's exist in each level of each object use a recursive function that matches using Array#findIndex() in sort callback

function sortChildren(main, other) {
  other.forEach((o, i) => {
    if (o.children) {
      const mChilds = main[i].children, oChilds = o.children;
      
      oChilds.sort((a, b) => {
        return mChilds.findIndex(main => main.Id === a.Id) - mChilds.findIndex(main => main.Id === b.Id)
      });
      // call function again on this level passing appropriate children arrays in
      sortChildren(mChilds, oChilds)
    }
  })
}

sortChildren(data, newData);
console.log(JSON.stringify(newData, null, ' '))

<script>
  var data = [{
    "Id": "1",
    "Children": [{
      "Id": "2",
      "Children": [{
          "Id": "3",
          "DisplayName": "1-2",
        },
        {
          "Id": "4",
          "DisplayName": "3-4",
        },
      ]
    }]
  }]

  var newData = [{
    "Id": "1",
    "Children": [{
      "Id": "2",
      "Children": [{
          "Id": "4",
          "DisplayName": "3-4",
        },
        {
          "Id": "3",
          "DisplayName": "1-2",
        },
      ]
    }]
  }]

</script>

这篇关于最有效的方式是通过另一个深度嵌套的对象数组对一个深度嵌套的对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆