英文:
Flat JSON to JSON with children
问题
这是您的代码,我将只翻译代码部分,不包括注释:
// 创建顶层节点的根数组
const root = [];
sideNavData.forEach((node, index) => {
const nodeLevel = node.metaData.level;
if (root.length === 0) {
return root.push(node)
}
const previousLevel = root[index - 1].metaData.level;
if (previousLevel === nodeLevel) {
return root.push(node)
} else {
return root[index - 1]['subNav'] = node
}
});
console.log(root);
如果您需要更多帮助或改进建议,请告诉我。
英文:
I have a JSON containing the following structure:
const content = [
{ title: "Item 1", metaData: { "level": 1, "desc": "Some Desc 1", "displayOnOverview": true }},
{ title: "Item 2", metaData: { "level": 2, "desc": "Some Desc 2", "displayOnOverview": true }},
{ title: "Item 3", metaData: { "level": 2, "desc": "Some Desc 3", "displayOnOverview": false }},
{ title: "Item 4", metaData: { "level": 3, "desc": "Some Desc 4", "displayOnOverview": true }},
{ title: "Item 5", metaData: { "level": 1, "desc": "Some Desc 5", "displayOnOverview": true }}
];
and from the above, I need to build the following output:
{
"title": "Item 1",
"metaData": {
"desc": "Some Desc 1",
"displayOnOverview": true
},
"subNav": [
{
"title": "Item 2",
"metaData": {
"desc": "Some Desc 2",
"displayOnOverview": true
}
},
{
"title": "Item 3",
"metaData": {
"desc": "Some Desc 3",
"displayOnOverview": false
},
"subNav": [
{
"title": "Item 4",
"metaData": {
"desc": "Some Desc 4",
"displayOnOverview": true
}
}
]
}
]
},
{
"title": "Item 6",
"metaData": {
"desc": "Some Desc 6",
"displayOnOverview": true
}
}
I have written some code (shown below) and it works but just for the top level, as soon as it finds "title": "Item 3"
it stops working, pretty sure I am missing something:
// Create root for top-level node(s)
const root = [];
sideNavData.forEach((node, index) => {
const nodeLevel = node.metaData.level;
if (root.length === 0) {
return root.push(node)
}
const previousLevel = root[index - 1].metaData.level;
if (previousLevel === nodeLevel) {
return root.push(node)
} else {
return root[index - 1]['subNav'] = node
}
});
console.log(root);
Can I get some ideas on how to achieve this?
答案1
得分: 1
以下是您要翻译的内容:
const content = [
{ title: "Item 1", metaData: { "level": 1 }},
{ title: "Item 2", metaData: { "level": 2 }},
{ title: "Item 3", metaData: { "level": 2 }},
{ title: "Item 4", metaData: { "level": 3 }},
{ title: "Item 5", metaData: { "level": 1 }}
];
function buildNestedStructure(data) {
const topLevelItems = [];
const itemMap = {};
for (const item of data) {
const { title, metaData } = item;
const newItem = { title, metaData };
if (metaData.level === 1) {
topLevelItems.push(newItem);
} else {
const parentLevel = metaData.level - 1;
const parentItem = itemMap[parentLevel];
if (!parentItem.subNav) {
parentItem.subNav = [];
}
parentItem.subNav.push(newItem);
}
itemMap[metaData.level] = newItem;
}
return topLevelItems;
}
const output = buildNestedStructure(content);
console.log(JSON.stringify(output, null, 2));
希望这对您有帮助!
英文:
Here you are:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const content = [
{ title: "Item 1", metaData: { "level": 1 }},
{ title: "Item 2", metaData: { "level": 2 }},
{ title: "Item 3", metaData: { "level": 2 }},
{ title: "Item 4", metaData: { "level": 3 }},
{ title: "Item 5", metaData: { "level": 1 }}
];
function buildNestedStructure(data) {
const topLevelItems = [];
const itemMap = {};
for (const item of data) {
const { title, metaData } = item;
const newItem = { title, metaData };
if (metaData.level === 1) {
topLevelItems.push(newItem);
} else {
const parentLevel = metaData.level - 1;
const parentItem = itemMap[parentLevel];
if (!parentItem.subNav) {
parentItem.subNav = [];
}
parentItem.subNav.push(newItem);
}
itemMap[metaData.level] = newItem;
}
return topLevelItems;
}
const output = buildNestedStructure(content);
console.log(JSON.stringify(output, null, 2));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论