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

D Durham

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 :

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.

mpm

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. 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to restructure a JSON object?

How to restructure objects of data into arrays of objects?

How to restructure the JSON Object with new key values?

creating a parent / child json object from a large array of path data in javascript

How to add a parent to every child in a json array?

How to filter and restructure json data with php?

Restructure a JSON object

Restructure JSON object

Best way restructure object in JS

How to get immediate parent Id of the child id in array of nested json Object?

How to restructure an array of single-element associative arrays into grouped subarrays?

Restructure json data

How to restructure array of objects to a nested array of object in JS?

How to restructure object of NSDictionaries

How to separate child array from json object

How to merge arrays in an array of JSON object?

How to restructure a multidimenstional array

How to create a tree (parent-child) object from Array in Javascript

How pass object of array child to parent with Typescript in ReactJS

How to restructure data

How to get parent JSON object based on child value in JS

How can I restructure object to Array of objects to a specific format?

How to create separate arrays from a parent object array

how to restructure json object to convert from one form to another form

Recursive function to restructure an array of object

How to convert parent child array to json tree structure by javascript

How modify each Child object with adding data from Parent object when having the foreign key (from Parent) inside child Object?

How to create Tree with checkboxes using JSON data with Parent Child Relation?

How to retrieve the data from a Parent/Child Heirarchal json in python?