I'm looking for a way to reorder an array in javascript moving elements to a specific position.
My array is:
[
{
"id": 105142,
"parent": null,
"created": "2015-03-20T17:21:33.013+01:00"
},
{
"id": 105150,
"parent": null,
"created": "2015-03-20T19:02:09.079+01:00"
},
{
"id": 105160,
"parent": null,
"created": "2015-03-20T22:15:06.543+01:00"
},
{
"id": 105190,
"parent": null,
"created": "2015-03-21T19:39:19.441+01:00"
},
{
"id": 105193,
"parent": null,
"created": "2015-03-22T01:42:00.974+01:00"
},
{
"id": 105195,
"parent": 105193,
"created": "2015-03-22T11:05:20.033+01:00"
},
{
"id": 105197,
"parent": null,
"created": "2015-03-22T11:06:15.241+01:00"
},
{
"id": 105203,
"parent": 105193,
"created": "2015-03-22T22:22:48.833+01:00"
}
]
I have to move items where parent !== null under their parent. The result so will be:
[
{
"id": 105142,
"parent": null,
"created": "2015-03-20T17:21:33.013+01:00"
},
{
"id": 105160,
"parent": 105142,
"created": "2015-03-20T22:15:06.543+01:00"
},
{
"id": 105150,
"parent": null,
"created": "2015-03-20T19:02:09.079+01:00"
},
{
"id": 105190,
"parent": null,
"created": "2015-03-21T19:39:19.441+01:00"
},
{
"id": 105193,
"parent": null,
"created": "2015-03-22T01:42:00.974+01:00"
},
{
"id": 105195,
"parent": 105193,
"created": "2015-03-22T11:05:20.033+01:00"
},
{
"id": 105203,
"parent": 105193,
"created": "2015-03-22T22:22:48.833+01:00"
},
{
"id": 105197,
"parent": null,
"created": "2015-03-22T11:06:15.241+01:00"
}
]
Is there any js library to move efficiently the items without a lot of recursions?