I have infinite levels array in each object and I want to add "id" based on levels. Suppose if level 1 then id should be 1 and level two then id will be 2 etc.
{
"name": "Anything2",
"code": "SS_1",
"levels": [
{
"levelName": "New level",
"levels": [
{
"levelName": "New Level2",
"levels": [
{
"levelName": "New Level2",
{
"levelName": "New Level2",
"levels": [
{
"levelName": "New level"
}
]
}
},
{
"levelName": "New Level2",
},
{
"levelName": "New Level2",
}
]
},
{
"levelName": "New Level2"
},
{
"levelName": "New Level2",
"levels": [
{
"levelName": "New level"
}
]
}
]
}
]
}
I want to convert the above array into below new array. I have tried using for loop but its not working I am not getting expected data. I want to add "id" based on levels. Suppose if level 1 then id should be 1 and level two then id will be 2 etc.
{
"name": "Anything2",
"code": "SS_1",
"levels": [
{
"level": 1,
"levelName": "New level",
"levels": [
{
"level": 2,
"levelName": "New Level2",
"levels": [
{
"level": 3,
"levelName": "New Level2",
{
"levelName": "New Level2",
"levels": [
{
"level": 4,
"levelName": "New level"
}
]
}
},
{
"level": 3,
"levelName": "New Level2",
},
{
"level": 3,
"levelName": "New Level2",
}
]
},
{
"level": 2,
"levelName": "New Level2"
},
{
"level": 2,
"levelName": "New Level2",
"levels": [
{
"level": 3,
"levelName": "New level"
}
]
}
]
}
]
}
You could take a recursive approach and hand over an incremented level for each level.
const
addLevel = (level = 0) => ({ levels = [], ...o }) =>
({ level, ...o, levels: levels.map(addLevel(level + 1)) }),
data = ...,
result = addLevel()(data);
Here I call the property "depth":
const data = { "name": "Anything2", "code": "SS_1", "levels": [{ "levelName": "New level", "levels": [{ "levelName": "New Level2", "levels": [{ "levelName": "New Level2" }, { "levelName": "New Level2", "levels": [{ "levelName": "New level" }] }, { "levelName": "New Level2" }, { "levelName": "New Level2" } ] }, { "levelName": "New Level2" }, { "levelName": "New Level2", "levels": [{ "levelName": "New level" }] } ] }] }; function addleveldepth(arr, depth = 1) { arr.forEach(obj => { obj.depth = depth; if (obj.levels) { addleveldepth(obj.levels, depth + 1); } }); } addleveldepth(data.levels); console.log(data);
Think it works:
const data = {"name": "Anything2","code": "SS_1","levels": [{"levelName": "New level","levels": [{"levelName": "New Level2","levels": [{"levelName": "New Level2","levels": [{"levelName": "New level"}]},{"levelName": "New Level2",},{"levelName": "New Level2",}]},{"levelName": "New Level2"},{"levelName": "New Level2","levels": [{"levelName": "New level"}]}]}]}; const iter = (arr, level) => arr.map((obj) => Array.isArray(obj.levels)? { level, ...obj, levels: iter(obj.levels, level + 1) }: { level, ...obj }); const result = {...data, levels: iter(data.levels, 1) }; console.dir(result, {depth: null})
.as-console-wrapper{min-height: 100%;important: top: 0}
Here is a simple solution using a stack and a while loop.
var tree = { children: [{ children: [{ children: [] }, { children: [] }] }, { children: [{ children: [] }] }] }; var stack = [ [tree, 0] // `0` is the `i` below ]; var n; while (n = stack.length) { let [node, i] = stack[n - 1]; if (i < node.children.length) { stack.push([node.children[i], 0]); stack[n - 1][1]++; // increment `i` } else { stack.pop(); node.depth = n; } } console.log(tree);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.