编号遵循以下规则:
如果某个项目位于级别 1,则其编号就是其在级别 1 中的位置。
如果某项的级别高于前一项,则它成为前一项的子项,并且其编号是前一项编号的扩展,并带有附加段。
如果一个项目与前一个项目处于同一级别,则它被视为前一个项目的同级。它的编号是通过增加前一项编号的最后一段来形成的。
如果某个项目的级别低于前一个项目,则它成为同一级别的最近祖先的子项。它的编号是通过重置其级别的编号并递增来形成的。
当您遍历项目列表时,这些规则适用。编号系统可以清晰地表示结构中项目之间的层次关系。
如果输入的json是
const jsonData1 = [
{ "name": 'Fruit', "level": 1 },
{ "name": 'Apple', "level": 2 },
{ "name": 'Banana', "level": 3 },
{ "name": 'Fruit loops', "level": 2 },
{ "name": 'Vegetables', "level": 3 },
{ "name": 'Green', "level": 4 },
{ "name": 'Green', "level": 4 },
{ "name": 'Green', "level": 5 },
{ "name": 'Orange', "level": 1 },
{ "name": 'Broccoli', "level": 1 },
{ "name": 'Brussels sprouts', "level": 2 },
{ "name": 'Pumpkins', "level": 2 },
{ "name": 'Carrots', "level": 3 }
];
输出是使用级别生成全局遵循的层次结构
[
{ name: 'Fruit', level: 1, number: '1' },
{ name: 'Apple', level: 2, number: '1.1' },
{ name: 'Banana', level: 3, number: '1.1.1' },
{ name: 'Fruit loops', level: 2, number: '1.2' },
{ name: 'Vegetables', level: 3, number: '1.2.1' },
{ name: 'Green', level: 4, number: '1.2.1.1' },
{ name: 'Green', level: 4, number: '1.2.1.2' },
{ name: 'Green', level: 5, number: '1.2.1.2.1' },
{ name: 'Orange', level: 1, number: '2' },
{ name: 'Broccoli', level: 1, number: '3' },
{ name: 'Brussels sprouts', level: 2, number: '3.1' },
{ name: 'Pumpkins', level: 2, number: '3.2' },
{ name: 'Carrots', level: 3, number: '3.2.1' }
]
我正在获得输出
"[{'name':'Fruit','level':1,'number':'1'},{'name':'Apple','level':2,'number':'1.1'},{'name':'Banana','level':3,'number':'1.1.1'},{'name':'Fruit loops','level':2,'number':'1.2'},{'name':'Vegetables','level':3,'number':'1.2.2'},{'name':'Green','level':4,'number':'1.2.2.1'},{'name':'Green','level':4,'number':'1.2.2.2'},{'name':'Green','level':5,'number':'1.2.2.2.1'},{'name':'Orange','level':1,'number':'2'},{'name':'Broccoli','level':1,'number':'3'},{'name':'Brussels sprouts','level':2,'number':'3.3'},{'name':'Pumpkins','level':2,'number':'3.4'},{'name':'Carrots','level':3,'number':'3.4.3'}]"
有人可以解释一下我的逻辑失败的地方并解释一下吗?
感谢您的回复。
我的代码是
function generateHierarchicalNumbering(data) {
const numbering = [];
const levelCount = {};
for (const item of data) {
const currentLevel = item.level;
if (!(currentLevel in levelCount)) {
levelCount[currentLevel] = 1;
} else {
levelCount[currentLevel]++;
}
let currentNumber = '';
for (let i = 1; i <= currentLevel; i++) {
if (i > 1) currentNumber += '.';
currentNumber += levelCount[i] || '1';
}
item.number = currentNumber;
numbering.push({ ...item });
}
return numbering;
}
// Test cases
const jsonData1 = [
{ "name": 'Fruit', "level": 1 },
{ "name": 'Apple', "level": 2 },
{ "name": 'Banana', "level": 3 },
{ "name": 'Fruit loops', "level": 2 },
{ "name": 'Vegetables', "level": 3 },
{ "name": 'Green', "level": 4 },
{ "name": 'Green', "level": 4 },
{ "name": 'Green', "level": 5 },
{ "name": 'Orange', "level": 1 },
{ "name": 'Broccoli', "level": 1 },
{ "name": 'Brussels sprouts', "level": 2 },
{ "name": 'Pumpkins', "level": 2 },
{ "name": 'Carrots', "level": 3 }
];
console.log(JSON.stringify(generateHierarchicalNumbering(jsonData1)));
每个级别没有一个单一的数字,而是每个家长都有一个数字。您可以使用堆栈来存储必要的信息。
function generateHierarchicalNumbering(data) {
const stack = [{level: 0, cnt: 0}];
return data.map(o => {
while (stack.at(-1)?.level > o.level - 1) stack.pop();
const top = stack.at(-1);
const curr = {...o, number: (top.number ? top.number + '.' : '') + ++top.cnt};
stack.push({...curr, cnt: 0});
return curr;
});
}
const data = [
{ "name": 'Fruit', "level": 1 },
{ "name": 'Apple', "level": 2 },
{ "name": 'Banana', "level": 3 },
{ "name": 'Fruit loops', "level": 2 },
{ "name": 'Vegetables', "level": 3 },
{ "name": 'Green', "level": 4 },
{ "name": 'Green', "level": 4 },
{ "name": 'Green', "level": 5 },
{ "name": 'Orange', "level": 1 },
{ "name": 'Broccoli', "level": 1 },
{ "name": 'Brussels sprouts', "level": 2 },
{ "name": 'Pumpkins', "level": 2 },
{ "name": 'Carrots', "level": 3 }
];
console.log(generateHierarchicalNumbering(data));