简体   繁体   English

最有效的方式是通过另一个深度嵌套的对象数组对一个深度嵌套的对象数组进行排序

[英]Most performant way to sort a deeply nested array of objects by another deeply nested array of objects

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. 例如,我包含一个元素数组 ,其中包含一个对象 ,该对象具有一个Children键,而Children键是一个对象数组,每个对象也都有自己的Children键,后者包含另一个数组。

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

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. 我想将第二个对象数组 与第一个对象数组进行比较 ,以确保第一个数组与第二个对象数组顺序相同 ,如果不是,则排序直到它。

Here is the second array: 这是第二个数组:

[
  {
    "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 eg 我目前正在尝试使用实用程序方法将第二个数组的每个元素转换为对象的键控对象,例如

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

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. 已经排序的数组的顺序不是基于ID的-它是任意的。 So the order needs to be preserved regardless. 因此,无论如何都需要保留订单。

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 假设深度相同并且每个对象的每个级别中都存在ID,则使用递归函数,该函数在sort回调中使用Array#findIndex()进行匹配

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM