Home > OS >  Sort recursive category array and attach subcategories inside de parents like a tree
Sort recursive category array and attach subcategories inside de parents like a tree

Time:02-27

i'm trying to group this array of categories/subcategories, but i'm not able to create a new array with the nested subcategories. i have this:

let categories = [
{
    "category": "/Heavy equipment",
    "parent": "/",
    "name": "Heavy equipment"
},
{
    "category": "/Heavy equipment/Mini Excavators",
    "parent": "/Heavy equipment",
    "name": "Mini Excavators"
},
{
    "category": "/Heavy equipment/Excavators",
    "parent": "/Heavy equipment",
    "name": "Excavators"
},
{
    "category": "/Heavy equipment/Wheeled Excavators",
    "parent": "/Heavy equipment",
    "name": "Wheeled Excavators"
},
{
    "category": "/Heavy equipment/Dozers",
    "parent": "/Heavy equipment",
    "name": "Dozers"
},
{
    "category": "/Heavy equipment/Articulated Dump Trucks",
    "parent": "/Heavy equipment",
    "name": "Articulated Dump Trucks"
},
{
    "category": "/Heavy equipment/Motor Graders",
    "parent": "/Heavy equipment",
    "name": "Motor Graders"
},
{
    "category": "/Heavy equipment/Wheel Loaders",
    "parent": "/Heavy equipment",
    "name": "Wheel Loaders"
},
{
    "category": "/Heavy equipment/Pad Foot Compactor",
    "parent": "/Heavy equipment",
    "name": "Pad Foot Compactor"
},
{
    "category": "/Heavy equipment/Smooth Drum Compactor",
    "parent": "/Heavy equipment",
    "name": "Smooth Drum Compactor"
},
{
    "category": "/Heavy equipment/Loader Backhoes",
    "parent": "/Heavy equipment",
    "name": "Loader Backhoes"
},
{
    "category": "/Heavy equipment/Forestry Equipment",
    "parent": "/Heavy equipment",
    "name": "Forestry Equipment"
},
{
    "category": "/Heavy equipment/Tractor",
    "parent": "/Heavy equipment",
    "name": "Tractor"
},
{
    "category": "/Heavy equipment/Telehandler",
    "parent": "/Heavy equipment",
    "name": "Telehandler"
},
{
    "category": "/Heavy equipment/Track Skid steer",
    "parent": "/Heavy equipment",
    "name": "Track Skid steer"
},
{
    "category": "/Heavy equipment/Wheeled Skid Steer",
    "parent": "/Heavy equipment",
    "name": "Wheeled Skid Steer"
},
{
    "category": "/Heavy equipment/Track Loaders",
    "parent": "/Heavy equipment",
    "name": "Track Loaders"
},
{
    "category": "/Heavy equipment/Snow Groomers",
    "parent": "/Heavy equipment",
    "name": "Snow Groomers"
},
{
    "category": "/Heavy equipment/Mining Equipment",
    "parent": "/Heavy equipment",
    "name": "Mining Equipment"
},
{
    "category": "/Heavy equipment/Pipelayers",
    "parent": "/Heavy equipment",
    "name": "Pipelayers"
},
{
    "category": "/Heavy equipment/Paving Equipment",
    "parent": "/Heavy equipment",
    "name": "Paving Equipment"
},
{
    "category": "/Heavy equipment/UTV's",
    "parent": "/Heavy equipment",
    "name": "UTV's"
},
{
    "category": "/Heavy equipment/Attachments",
    "parent": "/Heavy equipment",
    "name": "Attachments"
},
{
    "category": "/Heavy equipment/Miscellaneous",
    "parent": "/Heavy equipment",
    "name": "Miscellaneous"
},
{
    "category": "/Heavy equipment/test",
    "parent": "/Heavy equipment",
    "name": "test"
},
{
    "category": "/Heavy equipment/test 2",
    "parent": "/Heavy equipment",
    "name": "test 2"
},
{
    "category": "/Heavy equipment/Tractorcitos",
    "parent": "/Heavy equipment",
    "name": "Tractorcitos"
},
{
    "category": "/Heavy equipment/test 4",
    "parent": "/Heavy equipment",
    "name": "test 4"
},
{
    "category": "/Heavy equipment/test 4",
    "parent": "/Heavy equipment",
    "name": "test 4"
},
{
    "category": "/Heavy equipment/testCategory1",
    "parent": "/Heavy equipment",
    "name": "test 1"
},
{
    "category": "/Heavy equipment/testCategory1",
    "parent": "/Heavy equipment",
    "name": "test 1"
},
{
    "category": "/Heavy equipment/testCategory1",
    "parent": "/Heavy equipment",
    "name": "test 1"
},
{
    "category": "/Heavy equipment/Mini Excavators/testCategory1",
    "parent": "/Heavy equipment/Mini Excavators",
    "name": "testCategory1"
},
{
    "category": "/testCategory2",
    "parent": "/",
    "name": "testCategory2"
},
{
    "category": "/Heavy equipment/Tractor/testCategory4",
    "parent": "/Heavy equipment/Tractor",
    "name": "testCategory4"
},
{
    "category": "/testCategory5",
    "parent": "/",
    "name": "testCategory5"
},
{

    "category": "/testCategory6",
    "parent": "/",
    "name": "testCategory6"
},
{
    "category": "/test6",
    "parent": "/",
    "name": "test6"
},
{
    "category": "/test6/test7",
    "parent": "/test6",
    "name": "test7"
},
{
    "category": "/test6/test7/test8",
    "parent": "/test6/test7",
    "name": "test8"
},
{
    "category": "/test6/test7/test8/test9",
    "parent": "/test6/test7/test8",
    "name": "test9"
}];

I want to achieve something like this:

[
{
    category: "/Heavy equipment",
    parent: "/",
    name: "Heavy equipment",
    subcategories: [{
        category: "/Heavy equipment/Mini Excavators",
        parent: "/Heavy equipmefnt",
        name: "Mini Excavators",
        subcategories: [{
            category: "/Heavy equipment/Mini Excavators/testCategory1",
            parent: "/Heavy equipment/Mini Excavators",
            name: "testCategory1"
        }]
    }, {
        category: "/Heavy equipment/Excavators",
        parent: "/Heavy equipment",
        name: "Excavators"
    },
    {
        category: "/Heavy equipment/Wheeled Excavators",
        parent: "/Heavy equipment",
        name: "Wheeled Excavators"
    },
    {
        category: "/Heavy equipment/Dozers",
        parent: "/Heavy equipment",
        name: "Dozers"
    },
    {
        category: "/Heavy equipment/Articulated Dump Trucks",
        parent: "/Heavy equipment",
        name: "Articulated Dump Trucks"
    },
    {
        category: "/Heavy equipment/Motor Graders",
        parent: "/Heavy equipment",
        name: "Motor Graders"
    },
    {
        category: "/Heavy equipment/Wheel Loaders",
        parent: "/Heavy equipment",
        name: "Wheel Loaders"
    },
    {
        category: "/Heavy equipment/Pad Foot Compactor",
        parent: "/Heavy equipment",
        name: "Pad Foot Compactor"
    },
    {
        category: "/Heavy equipment/Smooth Drum Compactor",
        parent: "/Heavy equipment",
        name: "Smooth Drum Compactor"
    },
    {
        category: "/Heavy equipment/Loader Backhoes",
        parent: "/Heavy equipment",
        name: "Loader Backhoes"
    },
    {
        category: "/Heavy equipment/Telehandler",
        parent: "/Heavy equipment",
        name: "Telehandler"
    },
    {
        category: "/Heavy equipment/Track Skid steer",
        parent: "/Heavy equipment",
        name: "Track Skid steer"
    },
    {
        category: "/Heavy equipment/Wheeled Skid Steer",
        parent: "/Heavy equipment",
        name: "Wheeled Skid Steer"
    },
    {
        category: "/Heavy equipment/Track Loaders",
        parent: "/Heavy equipment",
        name: "Track Loaders"
    },
    {
        category: "/Heavy equipment/Snow Groomers",
        parent: "/Heavy equipment",
        name: "Snow Groomers"
    },
    {
        category: "/Heavy equipment/Mining Equipment",
        parent: "/Heavy equipment",
        name: "Mining Equipment"
    },
    {
        category: "/Heavy equipment/Pipelayers",
        parent: "/Heavy equipment",
        name: "Pipelayers"
    },
    {
        category: "/Heavy equipment/Paving Equipment",
        parent: "/Heavy equipment",
        name: "Paving Equipment"
    },
    {
        category: "/Heavy equipment/UTV's",
        parent: "/Heavy equipment",
        name: "UTV's"
    },
    {
        category: "/Heavy equipment/Attachments",
        parent: "/Heavy equipment",
        name: "Attachments"
    },
    {
        category: "/Heavy equipment/Miscellaneous",
        parent: "/Heavy equipment",
        name: "Miscellaneous"
    },
    {
        category: "/Heavy equipment/test",
        parent: "/Heavy equipment",
        name: "test"
    },
    {
        category: "/Heavy equipment/test 2",
        parent: "/Heavy equipment",
        name: "test 2"
    },
    {
        category: "/Heavy equipment/Tractorcitos",
        parent: "/Heavy equipment",
        name: "Tractorcitos"
    },
    {
        category: "/Heavy equipment/test 4",
        parent: "/Heavy equipment",
        name: "test 4"
    },
    {
        category: "/Heavy equipment/Forestry Equipment",
        parent: "/Heavy equipment",
        name: "Forestry Equipment"
    },
    {
        category: "/Heavy equipment/test 4",
        parent: "/Heavy equipment",
        name: "test 4"
    },
    {
        category: "/Heavy equipment/testCategory1",
        parent: "/Heavy equipment",
        name: "test 1"
    },
    {
        category: "/Heavy equipment/testCategory1",
        parent: "/Heavy equipment",
        name: "test 1"
    },
    {
        category: "/Heavy equipment/testCategory1",
        parent: "/Heavy equipment",
        name: "test 1"
    }, {
        category: "/Heavy equipment/Tractor",
        parent: "/Heavy equipment",
        name: "Tractor",
        subcategories: [{
            category: "/Heavy equipment/Tractor/testCategory4",
            parent: "/Heavy equipment/Tractor",
            name: "testCategory4"
        }]
    }]
},
{
    category: "/testCategory2",
    parent: "/",
    name: "testCategory2"
},

{
    category: "/testCategory5",
    parent: "/",
    name: "testCategory5"
},
{

    category: "/testCategory6",
    parent: "/",
    name: "testCategory6"
},
{
    category: "/test6",
    parent: "/",
    name: "test6",
    subcategories: [{
        category: "/test6/test7",
        parent: "/test6",
        name: "test7",
        subcategories: [{
            category: "/test6/test7/test8",
            parent: "/test6/test7",
            name: "test8",
            subcategories: [{
                category: "/test6/test7/test8/test9",
                parent: "/test6/test7/test8",
                name: "test9"
            }]
        }]
    }]
}];

I figured out sorting, I tried with for each, but i didn't get any luck :(, I used recursive functions and Buble method, but nothing, any ideas?, thanks in advance

CodePudding user response:

let categories = [
  {
    category: '/Heavy equipment',
    parent: '/',
    name: 'Heavy equipment',
  },
  {
    category: '/Heavy equipment/Mini Excavators',
    parent: '/Heavy equipment',
    name: 'Mini Excavators',
  },
  {
    category: '/Heavy equipment/Excavators',
    parent: '/Heavy equipment',
    name: 'Excavators',
  },
  {
    category: '/Heavy equipment/Wheeled Excavators',
    parent: '/Heavy equipment',
    name: 'Wheeled Excavators',
  },
  {
    category: '/Heavy equipment/Dozers',
    parent: '/Heavy equipment',
    name: 'Dozers',
  },
  {
    category: '/Heavy equipment/Articulated Dump Trucks',
    parent: '/Heavy equipment',
    name: 'Articulated Dump Trucks',
  },
  {
    category: '/Heavy equipment/Motor Graders',
    parent: '/Heavy equipment',
    name: 'Motor Graders',
  },
  {
    category: '/Heavy equipment/Wheel Loaders',
    parent: '/Heavy equipment',
    name: 'Wheel Loaders',
  },
  {
    category: '/Heavy equipment/Pad Foot Compactor',
    parent: '/Heavy equipment',
    name: 'Pad Foot Compactor',
  },
  {
    category: '/Heavy equipment/Smooth Drum Compactor',
    parent: '/Heavy equipment',
    name: 'Smooth Drum Compactor',
  },
  {
    category: '/Heavy equipment/Loader Backhoes',
    parent: '/Heavy equipment',
    name: 'Loader Backhoes',
  },
  {
    category: '/Heavy equipment/Forestry Equipment',
    parent: '/Heavy equipment',
    name: 'Forestry Equipment',
  },
  {
    category: '/Heavy equipment/Tractor',
    parent: '/Heavy equipment',
    name: 'Tractor',
  },
  {
    category: '/Heavy equipment/Telehandler',
    parent: '/Heavy equipment',
    name: 'Telehandler',
  },
  {
    category: '/Heavy equipment/Track Skid steer',
    parent: '/Heavy equipment',
    name: 'Track Skid steer',
  },
  {
    category: '/Heavy equipment/Wheeled Skid Steer',
    parent: '/Heavy equipment',
    name: 'Wheeled Skid Steer',
  },
  {
    category: '/Heavy equipment/Track Loaders',
    parent: '/Heavy equipment',
    name: 'Track Loaders',
  },
  {
    category: '/Heavy equipment/Snow Groomers',
    parent: '/Heavy equipment',
    name: 'Snow Groomers',
  },
  {
    category: '/Heavy equipment/Mining Equipment',
    parent: '/Heavy equipment',
    name: 'Mining Equipment',
  },
  {
    category: '/Heavy equipment/Pipelayers',
    parent: '/Heavy equipment',
    name: 'Pipelayers',
  },
  {
    category: '/Heavy equipment/Paving Equipment',
    parent: '/Heavy equipment',
    name: 'Paving Equipment',
  },
  {
    category: "/Heavy equipment/UTV's",
    parent: '/Heavy equipment',
    name: "UTV's",
  },
  {
    category: '/Heavy equipment/Attachments',
    parent: '/Heavy equipment',
    name: 'Attachments',
  },
  {
    category: '/Heavy equipment/Miscellaneous',
    parent: '/Heavy equipment',
    name: 'Miscellaneous',
  },
  {
    category: '/Heavy equipment/test',
    parent: '/Heavy equipment',
    name: 'test',
  },
  {
    category: '/Heavy equipment/test 2',
    parent: '/Heavy equipment',
    name: 'test 2',
  },
  {
    category: '/Heavy equipment/Tractorcitos',
    parent: '/Heavy equipment',
    name: 'Tractorcitos',
  },
  {
    category: '/Heavy equipment/test 4',
    parent: '/Heavy equipment',
    name: 'test 4',
  },
  {
    category: '/Heavy equipment/test 4',
    parent: '/Heavy equipment',
    name: 'test 4',
  },
  {
    category: '/Heavy equipment/testCategory1',
    parent: '/Heavy equipment',
    name: 'test 1',
  },
  {
    category: '/Heavy equipment/testCategory1',
    parent: '/Heavy equipment',
    name: 'test 1',
  },
  {
    category: '/Heavy equipment/testCategory1',
    parent: '/Heavy equipment',
    name: 'test 1',
  },
  {
    category: '/Heavy equipment/Mini Excavators/testCategory1',
    parent: '/Heavy equipment/Mini Excavators',
    name: 'testCategory1',
  },
  {
    category: '/testCategory2',
    parent: '/',
    name: 'testCategory2',
  },
  {
    category: '/Heavy equipment/Tractor/testCategory4',
    parent: '/Heavy equipment/Tractor',
    name: 'testCategory4',
  },
  {
    category: '/testCategory5',
    parent: '/',
    name: 'testCategory5',
  },
  {
    category: '/testCategory6',
    parent: '/',
    name: 'testCategory6',
  },
  {
    category: '/test6',
    parent: '/',
    name: 'test6',
  },
  {
    category: '/test6/test7',
    parent: '/test6',
    name: 'test7',
  },
  {
    category: '/test6/test7/test8',
    parent: '/test6/test7',
    name: 'test8',
  },
  {
    category: '/test6/test7/test8/test9',
    parent: '/test6/test7/test8',
    name: 'test9',
  },
];
const sortCategories = (categories) => {
  const sortedCategories = [];
  const categoriesMap = {};
  /* Creating a map of the categories. */
  categories.forEach((category) => {
    categoriesMap[category.category] = category;
  });
  /* Adding the category to the parent's subcategories array. */
  categories.forEach((category) => {
    const parent = categoriesMap[category.parent];
    /* Checking if the parent exists. If it does, it will push the category to the parent's
subcategories array. If it doesn't, it will push the category to the sortedCategories array. */
    if (parent) {
      if (!parent.subcategories) {
        parent.subcategories = [];
      }
      parent.subcategories.push(category);
    } else {
      sortedCategories.push(category);
    }
  });
  return sortedCategories;
}

console.log(JSON.stringify(sortCategories(categories), null, 2));

  • Related