I have an array like below and sorting it using .localeCompare method.
Once the sorting is done, I want to hoist those items within the Array which are having children item true.
Can refer this jsfiddle for the code: https://jsfiddle.net/sarav4gs/npd2mchs/78/
var data = [
{
"item": "aaabb110",
"recID": 15
},
{
"item": "aaabbbccc1",
"recID": 16
},
{
"item": "ZTemplate 1",
"recID": null,
"children": [
{
"item": "zaa",
"recID": 70
},
{
"item": "Qualification 1",
"recID": 73
}]
},
{
"item": "bbaacc1",
"recID": 17
},
{
"item": "bbaa005",
"recID": 18
},
{
"item": "ATemplate 2",
"recID": null,
"children": [{
"item": "Qualification 1",
"recID": 83
},
{
"item": "Qualification 2",
"recID": 84
},
{
"item": "Custom",
"recID": 86
},
{
"item": "custom code",
"recID": 87
},
{
"item": "aaa",
"recID": 89
}
]
}
];
to achieve that I am trying to slice the items which has children and copying it in a new array later push it in a same array at the top. (I tried with .splice, .unshift), but in the output it copies complete array in the first argument instead just as objects.
var CQTemplates = [];
var newData = data.filter(function(item,i){
if(item.children){
var removeAt = data.indexOf(item);
var CQTemplateCodeItem = data.slice(removeAt, removeAt+1);
pushAt = CQTemplates.length;
CQTemplates[pushAt] = CQTemplateCodeItem[0];
}
return item.children == undefined;
})
Expected Output is something like this
Array [
"itemsWithChildifAny? - sorted",
"itemsWithChildifAny? - sorted",
"Item - sorted",
"Item - sorted",
"Item - sorted"
]
