Consider the array of objects below, sorted by the code
property in ascending ASCII order:
var codes = [
{ code: '01' },
{ code: '0101' },
{ code: '0102' },
{ code: '010201' },
{ code: '0103' },
{ code: '02' },
{ code: '0201' },
{ code: '0202' },
];
Is there a way to transform this into a nested array structure like so :
var nestedCodes = [
{
code: '01',
children: [
{ code: '0101' },
{
code: '0102',
children: [
{ code: '010201' }
]
},
{ code: '0103' }
]
},
{
code: '02',
children: [
{ code: '0201' },
{ code: '0202' }
]
}
];
The organization of codes involves combining multiple 0N
, where N
could range from 1 to 9. It's worth noting that the codes are retrieved from a server and may have additional properties apart from code
, such as title
, which is irrelevant to this issue.
The primary objective here is to create a suitable format for use with jsTree.