Similar question was asked previously here and here, there are npm-packages as well BUT - I am not able to find a JavaScript code snippet or npm package which would allow preserving the the order of the children
Consider following data structure:
var nodes = [{id: 'a', parent: null, children: ['c', 'b']},
{id: 'b', parent: 'a', children: []},
{id: 'c', parent: 'a', children: []}]
I would like to get a nested object like this:
var tree = {id: 'a', parent: null, children: [
{id: 'c', parent: 'a', children: []},
{id: 'b', parent: 'a', children: []}
]}
The important fact is, that since the order of the children of a is c, b, the resulting children array in the nested structure will preserve the order.