0👍
You can use recursion for this, Following code snippet will help you.
const tree = [
{
name: "hello",
children: [
{
name: "world",
children: [
{
name: "bye"
}
]
},
{
name: "hola"
}
]
},
{
name: "how",
children: [
{
name: "is",
children: [
{
name: "life"
}
]
}
]
}
];
function getData(tree) {
if (tree && typeof tree[0].children === "undefined") return tree[0].name;
var outputString = [];
for (let i = 0; i < tree.length; i++) {
if (typeof tree[i].children != "undefined") {
outputString.push(tree[i].name, getData(tree[i].children));
} else {
outputString.push(tree[i].name);
}
}
return outputString.toString().split(",");
}
console.log(getData(tree));
Now you have an array of name and you can iterate this array.
- [Vuejs]-[Vue warn]: Error in v-on handler: "ReferenceError: fb is not defined"
- [Vuejs]-Not able to use route.push, it errors 'Maximum call stack size exceeded'
Source:stackexchange.com