遞回函式不是我的強項,我很確定這就是這里需要的。
我有一個嵌套的 json 物件,它代表一個嵌套的清單它是使用mdast-util-gfm-task-list-item從 markdown 自動生成的
* AAA
* BBB
* CCC
* [x] DDD
* [ ] EEE
* FFF
{
"type": "root",
"children": [
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "AAA"
}
]
},
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "BBB"
}
]
}
]
}
]
}
]
},
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "CCC"
}
]
},
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "DDD"
}
]
},
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "EEE"
}
]
}
]
},
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "FFF"
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
我需要將其操作為以下更簡潔的格式:
{
"type" : "list",
"data" : {
"style" : "unordered",
"items" : [
{
"content": "AAA",
"checked" : null
"items": [
{
"content": "BBB",
"checked" : null
"items": []
}
]
},
{
"content": "CCC",
"checked" : null
"items": [
{
"content": "DDD",
"checked": true,
"items": [
{
"content": "EEE",
"checked": false,
"items": []
},
{
"content": "FFF",
"checked": null,
"items": []
}
]
},
]
}
]
}
}
非常感謝任何幫助 - 我已經把頭撞在墻上幾個小時了。
uj5u.com熱心網友回復:
我想我離這里很近,但無法完成它:
function transform(data) {
const {type} = data;
if (type === "root") {
return transform(data.children[0]);
} else if (type === "list") {
return {
type,
data: {
style: data.ordered ? "ordered" : "unordered",
items: data.children.map(child => transform(child))
}
}
} else if (type === "listItem") {
return {
type,
checked: data.checked,
content: data.children[0].children[0].value,
items: data.children.filter(child => child.type === "list").map(child => transform(child))
}
}
return data;
}
// Pass your data in here as fromData
const toData = transform(fromData);
// toData would then be the new format
uj5u.com熱心網友回復:
謝謝@jimTheDev
我還在另一個論壇上獲得了一些幫助,兩個答案都很有用。最后,我成功地生成了以下函式,它幾乎完美地適用于我的用例。
let ast_to_ejs = (input) => {
const { type, children } = input;
if (type === "root") {
const data = {};
data.style = input.ordered ? 'ordered' : 'unordered';
data.items = children.map(ast_to_ejs);
return { type: 'list', data };
} else if (type === 'list') {
return children.map(ast_to_ejs);
} else if (type === 'listItem') {
const checked = input.checked;
const content = children[0].children[0].value;
const items = children[1] ? ast_to_ejs(children[1]) : [];
return { content, checked, items };
}
};
uj5u.com熱心網友回復:
我一直在研究的版本與您自己發現的相似,但它以不同的編碼風格完成,它從 中洗掉了一層陣列/root/data/items,這在您請求的輸出中不存在并且沒有多大意義.
const convert = ({type, children, ordered, checked}) =>
type == 'root'
? {
type: 'list',
data: {style: ordered ? 'ordered' : 'unordered', items: convert (children [0])}
}
: type == 'list'
? children .map (convert)
: type == 'listItem'
? {
content: children [0] .children [0] .value,
checked: checked,
items: children [1] ? convert (children [1]) : []
}
: {error: `unknown type: '${type}'`}
const ast = {type: "root", children: [{type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "AAA"}]}, {type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "BBB"}]}]}]}]}, {type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "CCC"}]}, {type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: true, children: [{type: "paragraph", children: [{type: "text", value: "DDD"}]}, {type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: false, children: [{type: "paragraph", children: [{type: "text", value: "EEE"}]}]}, {type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "FFF"}]}]}]}]}]}]}]}]}
console .log (convert (ast))
.as-console-wrapper {max-height: 100% !important; top: 0}
我還更改了輸入以表示checked: trueforDDD和checked: falseforEEE以匹配您的示例標記。最初的 AST 對這null兩者都有。我假設這null意味著“這里沒有復選框”,true并且false如果有一個選中或未選中,就會使用它。如果不是這種情況,那么可能需要一些擺弄。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/461523.html
標籤:javascript 递归 github-flavored-markdown
下一篇:為Jenkins實體設定全域描述
