我正在嘗試組織 JSON 資料。這是我的 JSON:
let all = [
{
id: "n1",
name: "Hipokrat",
children: [
{
id: "n2",
name: "Edward Janner",
children: [
{
id: "n9",
name: "Edison, Thomas",
}
]
}
]
},
{
id: "n3",
name: "William Harvey",
children: [
{
id: "n10",
name: "Lister, Joseph",
children: [
{
id: "n11",
name: "Kant, Immanuel",
children: [
{
id: "n15",
name: "Rawls, John"
},
{
id: "n46",
name: "More, Thomas",
},
{
id: "n47",
name: "Galen",
}
]
}
]
},
{
id: "n12",
name: "Smith, Adam",
}
]
},
{
id: "n48",
name: "Osler, William",
children: [
{
id: "n51",
name: "Louis Pasteur",
}
]
},
{
id: "n52",
name: "John Hunter",
children: [
{
id: "n53",
name: "Freud, Sigmund",
}
]
}
];
在這里,我想找到 ID 為“n10”的節點并將它們移動到“William Harvey”和 ID 為“n3”的祖先節點。我們應該將這些添加到 children 陣列中。
所以我想要這個結果:
let all = [
{
id: "n1",
name: "Hipokrat",
children: [
{
id: "n2",
name: "Edward Janner",
children: [
{
id: "n9",
name: "Edison, Thomas",
}
]
}
]
},
{
id: "n3",
name: "William Harvey",
children: [
{
id: "n11",
name: "Kant, Immanuel",
children: [
{
id: "n15",
name: "Rawls, John"
},
{
id: "n46",
name: "More, Thomas",
},
{
id: "n47",
name: "Galen",
}
]
},
{
id: "n12",
name: "Smith, Adam",
}
]
},
{
id: "n48",
name: "Osler, William",
children: [
{
id: "n51",
name: "Louis Pasteur",
}
]
},
{
id: "n52",
name: "John Hunter",
children: [
{
id: "n53",
name: "Freud, Sigmund",
}
]
}
];
這是我的嘗試:
let all = [
{
id: "n1",
name: "Hipokrat",
children: [
{
id: "n2",
name: "Edward Janner",
children: [
{
id: "n9",
name: "Edison, Thomas",
}
]
}
]
},
{
id: "n3",
name: "William Harvey",
children: [
{
id: "n11",
name: "Kant, Immanuel",
children: [
{
id: "n15",
name: "Rawls, John"
},
{
id: "n46",
name: "More, Thomas",
},
{
id: "n47",
name: "Galen",
}
]
},
{
id: "n12",
name: "Smith, Adam",
}
]
},
{
id: "n48",
name: "Osler, William",
children: [
{
id: "n51",
name: "Louis Pasteur",
}
]
},
{
id: "n52",
name: "John Hunter",
children: [
{
id: "n53",
name: "Freud, Sigmund",
}
]
}
];
function isObject(variable){
if ( typeof variable === 'object' && !Array.isArray(variable) && variable !== null) {
return true;
} else{
return false;
}
}
function isArray(variable){
if(!isObject(variable) && Array.isArray(variable)){
return true;
} else{
return false;
}
}
function process(all, value, indexes = "", foundAndChildren = false) {
if(foundAndChildren){
return foundAndChildren;
}
if(isArray(all)){ // is Array
console.log("-> Children size: " all.length);
console.log("-PASSED WITH 1");
console.log("-----------------");
console.log("");
all.forEach(subElement => {
return process(subElement, value);
});
} else if(isObject(all)){ // is object
if(all["id"] == value){ // Match
console.log("#########");
console.log(all.id);
console.log(all.name);
console.log("--PASSED WITH 2");
let children = [];
if(all["children"] != undefined){
children = all.children
}
let res = {
id: value,
indexes: indexes,
children: children
};
console.log(res);
return res;
} else {
console.log(all.name);
console.log("--PASSED WITH 3");
console.log("");
if(isArray(all["children"])) { // is object but has children
console.log("->" all.name);
console.log("Children size:" all.children.length);
console.log("--PASSED WITH 4");
console.log("");
all["children"].forEach(elementOfChildren => {
return process(elementOfChildren, value);
});
}
}
}
}
console.log(process(all, "n10"));
這里是操場:https : //playcode.io/823402/
uj5u.com熱心網友回復:
你可以這樣做:
function replaceNode(jsonData, id) {
function replaceChildren(o, id) {
for (let key in o) {
const object = o[key]
if (object.id == id) {
if (object.children && object.children.length > 0) {
o.splice(key, 1, ...object.children)
return true
} else throw `id: ${id} has no children`
} else {
if (object.children && object.children.length > 0) {
const found = replaceChildren(object.children, id)
if (found) return found
}
}
}
}
const o = [...jsonData]
replaceChildren(o, id)
return o
}
然后傳遞json資料和節點id
const output = replaceNode(all, 'n10')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324348.html
標籤:javascript 节点.js json
上一篇:陣列的NEXT_OFFSET記錄
