简体   繁体   English

如何最好地将 JSON 子数据对象数组重组为父数组

[英]How best to restructure JSON child data object arrays into parent array

We have some JSON data where each parent array object can have a property that is an array of "children" along the lines of the following :我们有一些 JSON 数据,其中每个父数组对象都可以具有一个属性,该属性是一个“子”数组,如下所示:

data: [
  {
    value: 1,
    parent_id: null,
    label: "1.0 - TT One",
    children: [
      {
        value: 3,
        label: "1.1 - TT One-One",
        parent_id: 1,
      },
     {
        value: 4,
        label: "1.2 - TT One-Two",
        parent_id: 1,
     }
    ]
  },
  {
    value: 2,
    parent_id: null,
    label: "2.0 - TT Two",
    children: [
      {
        value: 5,
        label: "2.1 - TT Two-One",
        parent_id: 2,
      }
    ]
  }
]

We'd like to "flatten" the children so that we end up with one array that is all the parents and children as follows (it does not have to stay named data if not efficient):我们想“展平”孩子,以便我们最终得到一个包含所有父母和孩子的数组,如下所示(如果效率不高,则不必保留命名数据):

data: [
  {  
    value: 1,
    parent_id: null,
    label: "1.0 - TT One"
  },
  {  <-- FORMER CHILD
    value: 3,
    label: "1.1 - TT One-One",
    parent_id: 1
   },
   {  <-- FORMER CHILD
     value: 4,
     label: "1.2 - TT One-Two",
     parent_id: 1,
   },
   {
     value: 2,
     parent_id: null,
     label: "2.0 - TT Two"
   },
   {  <-- FORMER CHILD
     value: 5,
     label: "2.1 - TT Two-One",
     parent_id: 2,
   }
]  

Thoughts on how best to accomplish this in an efficient manor?关于如何在高效庄园中最好地实现这一目标的想法? We have underscore if that will help.如果这会有所帮助,我们有下划线。

Found a buttload of array to array flatten and some object to array flattens, but nothing combining the two.找到了一个数组来平展数组和一些对象来平展数组,但没有将两者结合起来。 If we had this, we wouldn't have needed to post.如果我们有这个,我们就不需要发布了。 If it's not obvious from exactly what we have to exactly what we need, what is the point?如果从我们所拥有的到我们真正需要的都不是很明显,那还有什么意义呢?

the point is to understand what is your data structure and how to visit every element of that data structure.关键是要了解您的数据结构是什么以及如何访问该数据结构的每个元素。

Since there are only 2 levels you do not even need to find a general solution to transform your initial data into an array.由于只有 2 个级别,因此您甚至不需要找到将初始数据转换为数组的通用解决方案。 Do you know how to traverse an array?你知道如何遍历数组吗? then you know how to traverse an element that has an array as property as well.那么您就知道如何遍历具有数组作为属性的元素。

Anyway here is both a recursive and a non recursive generalized solution.无论如何,这里既是递归的又是非递归的广义解决方案。

 var d = [{ value: 1, parent_id: null, label: "1.0 - TT One", children: [{ value: 3, label: "1.1 - TT One-One", parent_id: 1, }, { value: 4, label: "1.2 - TT One-Two", parent_id: 1, } ] }, { value: 2, parent_id: null, label: "2.0 - TT Two", children: [{ value: 5, label: "2.1 - TT Two-One", parent_id: 2, }] } ]; function walkRecursive(data) { let result = [] data.forEach(element => { let e = { ...element} result.push(e); if (e.children) { let children = walkRecursive(e.children) result = result.concat(children) delete e.children } }); return result; } function walk(data) { data = data.slice() let result = [] let d, oldData; while (d = data.shift()) { let el = { ...d} result.push(el) if (el.children) { oldData = data data = el.children.slice(); delete el.children; } else { if (oldData && data.length == 0) { data = oldData oldData = null; } } } return result; } console.log(walkRecursive(d)) console.log(walk(d))

https://codeburst.io/learn-and-understand-recursion-in-javascript-b588218e87ea https://codeburst.io/learn-and-understand-recursion-in-javascript-b588218e87ea

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

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