对递归类别数组进行排序并像树一样在父级中附加子类别
Sort recursive category array and attach subcategories inside de parents like a tree
我正在尝试对这个 categories/subcategories 数组进行分组,但我无法创建包含嵌套子类别的新数组。
我有这个:
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"
}];
我想实现这样的目标:
[
{
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"
}]
}]
}]
}];
我想出了排序,我尝试了每个排序,但我没有得到任何运气:(,我使用了递归函数和 Buble 方法,但什么都没有,有什么想法吗?提前致谢
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));
(注意: 以后的题,请把数据缩减到合理的大小,不是41个类别,5到10个就足够演示题目了,会更容易一起工作。)
基本递归可以过滤根元素,然后针对每个元素递归过滤其子类别,包括 subcategories
仅当找到任何节点时:
const nestCategories = (categories, target = '/') =>
categories .filter (({parent}) => parent == target) .map (
(item, _, __, subcategories = nestCategories (categories, item .category)) => ({
... item,
... (subcategories .length ? {subcategories} : {})
})
)
const 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"}]
console .log (nestCategories (categories))
.as-console-wrapper {max-height: 100% !important; top: 0}
我更喜欢使用尽可能少的局部变量并使用表达式而不是语句。但如果这更熟悉,它会以同样的方式工作:
const nestCategories = (categories, target = '/') =>
categories .filter (({parent}) => parent == target) .map (item => {
const subcategories = nestCategories (categories, item .category)
return {
... item,
... (subcategories .length ? {subcategories} : {})
}
})
而且我们要注意,在这个结构中,parent
节点现在是冗余信息,我们可以这样删除它:
const nestCategories = (categories, target = '/') =>
categories .filter (({parent}) => parent == target) .map (
({category, parent, ...rest}, _, __, subcategories = nestCategories (categories, category)) => ({
category,
... rest,
... (subcategories .length ? {subcategories} : {})
})
)
const 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"}]
console .log (nestCategories (categories))
.as-console-wrapper {max-height: 100% !important; top: 0}
或者您可以使用上面命令式更强的版本进行相同的转换。
我正在尝试对这个 categories/subcategories 数组进行分组,但我无法创建包含嵌套子类别的新数组。 我有这个:
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"
}];
我想实现这样的目标:
[
{
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"
}]
}]
}]
}];
我想出了排序,我尝试了每个排序,但我没有得到任何运气:(,我使用了递归函数和 Buble 方法,但什么都没有,有什么想法吗?提前致谢
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));
(注意: 以后的题,请把数据缩减到合理的大小,不是41个类别,5到10个就足够演示题目了,会更容易一起工作。)
基本递归可以过滤根元素,然后针对每个元素递归过滤其子类别,包括 subcategories
仅当找到任何节点时:
const nestCategories = (categories, target = '/') =>
categories .filter (({parent}) => parent == target) .map (
(item, _, __, subcategories = nestCategories (categories, item .category)) => ({
... item,
... (subcategories .length ? {subcategories} : {})
})
)
const 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"}]
console .log (nestCategories (categories))
.as-console-wrapper {max-height: 100% !important; top: 0}
我更喜欢使用尽可能少的局部变量并使用表达式而不是语句。但如果这更熟悉,它会以同样的方式工作:
const nestCategories = (categories, target = '/') =>
categories .filter (({parent}) => parent == target) .map (item => {
const subcategories = nestCategories (categories, item .category)
return {
... item,
... (subcategories .length ? {subcategories} : {})
}
})
而且我们要注意,在这个结构中,parent
节点现在是冗余信息,我们可以这样删除它:
const nestCategories = (categories, target = '/') =>
categories .filter (({parent}) => parent == target) .map (
({category, parent, ...rest}, _, __, subcategories = nestCategories (categories, category)) => ({
category,
... rest,
... (subcategories .length ? {subcategories} : {})
})
)
const 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"}]
console .log (nestCategories (categories))
.as-console-wrapper {max-height: 100% !important; top: 0}
或者您可以使用上面命令式更强的版本进行相同的转换。