0

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"
]
1
  • 1
    "Once the sorting is done, I want to hoist those items within the Array which are having children item true" - don't. Just sort them into the right place straight away! Commented May 1, 2021 at 21:28

1 Answer 1

1

You are trying to separate the objects with children and without children. You can try this,

Using ForEach

var CQTemplates = [];
let withChildren = [];
let withoutChildren = [];
data.forEach(function(item){
  if(item.children){
    withChildren.push(item);
  }  else {
    withoutChildren.push(item);
  }  
})

Using Filter

var withoutChildren = [];
var withChildren = data.filter(function(item,i){
    if(!item.children){
    withoutChildren.push(item);
    return false;
  }
  return true;
})

console.log('Combined array', [...withChildren, ...withoutChildren]);

Output will be like

output

Happy coding!!

Sign up to request clarification or add additional context in comments.

5 Comments

where is the splice() method my friend
You can achieve it without splice() method itself, I can also do that with splice(). But question is do we need that neccessarily?
since the QA said he needs a splice(), try to answer him with what he needs, we know in programming, we can achieve the same thing using million of directions
@user123456 Thanks for your answer. This actually serves the purpose.
as @sivaragala mentioned, If I were to solve by .splice(), what am I missing or doing wrong in my code to achieve that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.