1

I have an array of object. Each item has a paret id mentioned. I want to create a nested object where it nesting goes on as much as the subcategories. Input and expected output given below. How can I achieve this with plain JS?

var inputArr = [
  {
    category_id: 1,
    parent_category_id: 3,
    title: "category1"
  },
  {
    category_id: 2,
    parent_category_id: 1,
    title: "category2"
  },
  {
    category_id: 3,
    title: "category3"
  },
  {
    category_id: 4,
    parent_category_id: 1,
    title: "category4"
  },
  {
    category_id: 5,
    parent_category_id: 2,
    title: "category5"
  }
]


var expectedOutput = {
  category_id: 3,
  title: "category3",
  subcategories: [
    {
      category_id: 1,
      subcategories: [
       {
         category_id: 2,
         title: "category2",
         subcategories: [
           {
             category_id: 5,
             title: "category5"
           }
         ]
       },
       {
         category_id: 4,
         title: "category4"
       }
      ]
    }
  ]
}

2
  • The "how" will always involve writing some code. So what have you written so far? Commented Oct 7, 2016 at 1:20
  • I tried to do with a reduce, and also using loop. But, not getting any clue on doing the nesting. Since whatever I tried are all half way, should I really add that? I dont think, for that reason, you have to give a negative point Commented Oct 7, 2016 at 2:03

1 Answer 1

1

var inputArr = [
    {
        category_id: 1,
        parent_category_id: 3,
        title: "category1"
    },
    {
        category_id: 2,
        parent_category_id: 1,
        title: "category2"
    },
    {
        category_id: 3,
        title: "category3"
    },
    {
        category_id: 4,
        parent_category_id: 1,
        title: "category4"
    },
    {
        category_id: 5,
        parent_category_id: 2,
        title: "category5"
    }
];

var temp = [];
var parent;

for (var i = 0; i < inputArr.length; i++) {
  
  if (temp[inputArr[i].category_id]) {
    Object.assign(temp[inputArr[i].category_id], inputArr[i]);
  } else {
    temp[inputArr[i].category_id] = inputArr[i];
    // uncomment the following assignment to add empty subcategories
    // array to leaf nodes for consistency.
    // temp[inputArr[i].category_id].subcategories = [];
  }

  var parentId = inputArr[i].parent_category_id;
  if (!parentId) {
    parent = temp[inputArr[i].category_id];  
  } else {
    if (!temp[parentId]) {
      temp[parentId] = {
        category_id: parentId,
        parent_category_id: undefined,
        title: undefined,
        subcategories: [temp[inputArr[i].category_id]]
      }
    } else {
      temp[parentId].subcategories = temp[parentId].subcategories || [];
      temp[parentId].subcategories.push(temp[inputArr[i].category_id]);
    }
  }
  
  delete temp[inputArr[i].category_id].parent_category_id;
}

temp = undefined;

document.write('<code><pre>' + JSON.stringify(parent, null, 2) + '</pre></code>');

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

Comments

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.