[英]Json - Tree data format
我正在嘗試格式化 json reposnse 以使其與JStree一起使用
這里 url json 響應
{
"items": [
{ "id": 1, "parent_id": 0, "name": "Root Catalog" },
{ "id": 2, "parent_id": 1, "name": "Default Category" },
{ "id": 3, "parent_id": 2, "name": "category1" },
{ "id": 5, "parent_id": 3, "name": "Sub1_category1" },
{ "id": 6, "parent_id": 3, "name": "Sub2_category1" },
{ "id": 11, "parent_id": 2, "name": "category2" },
{ "id": 12, "parent_id": 2, "name": "category3" },
{ "id": 14, "parent_id": 12, "name": "Sub1_category3" },
{ "id": 15, "parent_id": 12, "name": "Sub1_category4" }
]
}
這是我想要得到的回應:
[
{ 'id': '1', 'parent': '#', 'text': 'Root Catalog' },
{ 'id': '2', 'parent': '1', 'text': 'Default Category' },
{ 'id': '3', 'parent': '2', 'text': 'category1' },
{ 'id': '5', 'parent': '3', 'text': 'Sub1_category1' },
{ 'id': '6', 'parent': '3', 'text': 'Sub2_category1' },
{ 'id': '11', 'parent': '2', 'text': 'category2' },
{ 'id': '12', 'parent': '2', 'text': 'category3' },
{ 'id': '14', 'parent': '12', 'text': 'Sub1_category3' },
{ 'id': '15', 'parent': '12', 'text': 'Sub2_category3' },
]
這是我的看法:
let test = {
"items": [
{ "id": 1, "parent_id": 0, "name": "Root Catalog" },
{ "id": 2, "parent_id": 1, "name": "Default Category" },
{ "id": 3, "parent_id": 2, "name": "category1" },
{ "id": 5, "parent_id": 3, "name": "Sub1_category1" },
{ "id": 6, "parent_id": 3, "name": "Sub2_category1" },
{ "id": 11, "parent_id": 2, "name": "category2" },
{ "id": 12, "parent_id": 2, "name": "category3" },
{ "id": 14, "parent_id": 12, "name": "Sub1_category3" },
{ "id": 15, "parent_id": 12, "name": "Sub1_category4" }
]
}
function formatData(itemsList) {
let formatOutput = [];
for (item of itemsList.items) {
if (item.parent_id > 0) {
if (item.parent_id !== 1 && typeof formatOutput[item.parent_id] === 'undefined') {
formatOutput[item.parent_id] = {
"parent": item.parent_id,
"id": item.id,
"text": null,
"children": [
{
id: item.id,
text: item.name,
parent: item.parent_id
}
]
}
} else if (item.parent_id !== 1) {
formatOutput[item.parent_id].children.push({
id: item.id,
text: item.name,
parent: item.parent_id
})
}
}
}
for (item of itemsList.items) {
if (typeof formatOutput[item.id] === 'object') {
formatOutput[item.id].text = item.name
}
}
return formatOutput.filter(val => val)
}
console.log(formatData(test))
2個問題:
1.Json 未格式化為 jstree 要求
我非常感謝您能提供的任何幫助。
我無法理解你的所有邏輯,但我希望這段代碼可以幫助你。
const test = {"items":[ {"id":1,"parent_id":0,"name":"Root Catalog"}, {"id":2,"parent_id":1,"name":"Default Category"}, {"id":3,"parent_id":2,"name":"category1"}, {"id":5,"parent_id":3,"name":"Sub1_category1"}, {"id":6,"parent_id":3,"name":"Sub2_category1"}, {"id":11,"parent_id":2,"name":"category2"}, {"id":12,"parent_id":2,"name":"category3"}, {"id":14,"parent_id":12,"name":"Sub1_category3"}, {"id":15,"parent_id":12,"name":"Sub1_category4"} ] } const result = test.items.map(x =>{ return { id: x.id === 1? "#": x.id, // if the id of the element is 1 put # else x.id parent: x.parent_id, text: x.name } }) console.log(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.