How to move objects in JS?
Type, I have a Json
{
children":[
{}
]
}
The question is, that sometimes there are objects inside objects and sometimes not, within that children, I can have other children, and so on.
How to move objects in JS?
Type, I have a Json
{
children":[
{}
]
}
The question is, that sometimes there are objects inside objects and sometimes not, within that children, I can have other children, and so on.
Here's a suggestion:
function criarSubPasta(obj, parent) {
// criar <li>nome</li>
var nameLi = document.createElement('li');
nameLi.innerHTML = obj.name;
parent.appendChild(nameLi);
// parar aqui se não houver children
if (!obj.children) return;
// preparar um novo <ul></ul> para as subpastas
var childrenLi = document.createElement('li');
var ul = document.createElement('ul');
parent.appendChild(childrenLi);
childrenLi.appendChild(ul);
obj.children.forEach(function(child) {
// correr a mesma lógica recursivamente nas subpastas
criarSubPasta(child, ul);
});
}
criarSubPasta(json, document.querySelector('ul'));
var json = [{
"id": "152",
"repository": "1",
"name": "Repositório",
"created_by": {
"id": "14",
"name": "",
"username": ""
},
"created_at": "",
"last_updated_by": {
"id": "6",
"name": "",
"username": ""
},
"last_updated_at": "",
"children": [{
"id": "688",
"repository": "1",
"parent": "152",
"name": "Licitações",
"created_by": "6",
"created_at": "2147483647",
"last_updated_by": "245",
"last_updated_at": "2147483647",
"active": "1",
"children": [{
"id": "722",
"repository": "1",
"parent": "688",
"name": "Federais",
"created_by": "6",
"created_at": "2147483647",
"last_updated_by": "7",
"last_updated_at": "2147483647",
"active": "1"
},
{
"id": "724",
"repository": "1",
"parent": "688",
"name": "Estaduais",
"created_by": "6",
"created_at": "2147483647",
"last_updated_by": "7",
"last_updated_at": "2147483647",
"active": "1"
},
{
"id": "740",
"repository": "1",
"parent": "688",
"name": "Municipais",
"created_by": "245",
"created_at": "2147483647",
"last_updated_by": "7",
"last_updated_at": "2147483647",
"active": "1",
"children": [{
"id": "778",
"repository": "1",
"parent": "740",
"name": "2017",
"created_by": "7",
"created_at": "1484311566",
"last_updated_by": "7",
"last_updated_at": "1484311566",
"active": "1"
}]
}
]
},
{
"id": "689",
"repository": "1",
"parent": "152",
"name": "Dossiê Funcional",
"created_by": "6",
"created_at": "2147483647",
"last_updated_by": "6",
"last_updated_at": "2147483647",
"active": "1",
"children": [{
"id": "704",
"repository": "1",
"parent": "689",
"name": "Matriz",
"created_by": "6",
"created_at": "2147483647",
"last_updated_by": "7",
"last_updated_at": "2147483647",
"active": "1"
},
{
"id": "718",
"repository": "1",
"parent": "689",
"name": "Filiais",
"created_by": "236",
"created_at": "2147483647",
"last_updated_by": "7",
"last_updated_at": "2147483647",
"active": "1"
}
]
},
{
"id": "690",
"repository": "1",
"parent": "152",
"name": "Notas Fiscais",
"created_by": "6",
"created_at": "2147483647",
"last_updated_by": "6",
"last_updated_at": "2147483647",
"active": "1",
"children": [{
"id": "697",
"repository": "1",
"parent": "690",
"name": "Serviço",
"created_by": "6",
"created_at": "2147483647",
"last_updated_by": "6",
"last_updated_at": "2147483647",
"active": "1"
},
{
"id": "698",
"repository": "1",
"parent": "690",
"name": "Material",
"created_by": "6",
"created_at": "2147483647",
"last_updated_by": "6",
"last_updated_at": "2147483647",
"active": "1"
},
{
"id": "699",
"repository": "1",
"parent": "690",
"name": "Despesas",
"created_by": "6",
"created_at": "2147483647",
"last_updated_by": "6",
"last_updated_at": "2147483647",
"active": "1"
}
]
}
]
}];
function criarSubPasta(obj, parent) {
var nameLi = document.createElement('li');
nameLi.innerHTML = obj.name;
parent.appendChild(nameLi);
if (!obj.children) return;
var childrenLi = document.createElement('li');
var ul = document.createElement('ul');
parent.appendChild(childrenLi);
childrenLi.appendChild(ul);
obj.children.forEach(function(child) {
criarSubPasta(child, ul);
});
}
json.forEach(function(obj) {
var ul = document.createElement('ul');
document.body.appendChild(ul);
criarSubPasta(obj, ul);
});