【发布时间】:2021-08-21 21:15:37
【问题描述】:
给定以下树:
{
"name": "svg",
"type": "element",
"value": "",
"attributes": {
"xmlns": "http://www.w3.org/2000/svg",
"width": "1440",
"height": "1080"
},
"children": [{
"name": "g",
"type": "element",
"value": "",
"attributes": {},
"children": [{
"name": "g",
"type": "element",
"value": "",
"attributes": {},
"children": [{
"name": "g",
"type": "element",
"value": "",
"attributes": {
"fill": "#627580"
},
"children": [{
"name": "g",
"type": "element",
"value": "",
"attributes": {
"stroke-linejoin": "round"
},
"children": [{
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}, {
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}, {
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}]
}, {
"name": "g",
"type": "element",
"value": "",
"attributes": {
"stroke-linejoin": "round"
},
"children": [{
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}, {
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}, {
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}]
}]
}]
}]
}]
}
...这是此 HTML 的表示:
<svg xmlns="http://www.w3.org/2000/svg" width="1440" height="1080">
<g>
<g>
<g fill="#627580">
<g stroke-linejoin="round">
<path d="M150 0 L75 200 L225 200 Z" />
<path d="M150 0 L75 200 L225 200 Z" />
<path d="M150 0 L75 200 L225 200 Z" />
</g>
<g stroke-linejoin="round">
<path d="M150 0 L75 200 L225 200 Z" />
<path d="M150 0 L75 200 L225 200 Z" />
<path d="M150 0 L75 200 L225 200 Z" />
</g>
</g>
</g>
</g>
</svg>
如何在 JavaScript 中递归地转换此数据结构,以便折叠只有一个子 <g> 的所有组 <g>,并将其子代分配给其父代?
预期的结果是这样的:
{
"name": "svg",
"type": "element",
"value": "",
"attributes": {
"xmlns": "http://www.w3.org/2000/svg",
"width": "1440",
"height": "1080"
},
"children": [{
"name": "g",
"type": "element",
"value": "",
"attributes": {},
"children": [{
"name": "g",
"type": "element",
"value": "",
"attributes": {
"stroke-linejoin": "round"
},
"children": [{
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}, {
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}, {
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}]
}, {
"name": "g",
"type": "element",
"value": "",
"attributes": {
"stroke-linejoin": "round"
},
"children": [{
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}, {
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}, {
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M150 0 L75 200 L225 200 Z"
},
"children": []
}]
}]
}]
}
...这是此 HTML 的表示:
<svg xmlns="http://www.w3.org/2000/svg" width="1440" height="1080">
<g>
<g fill="#627580">
<g stroke-linejoin="round">
<path d="M150 0 L75 200 L225 200 Z" />
<path d="M150 0 L75 200 L225 200 Z" />
<path d="M150 0 L75 200 L225 200 Z" />
</g>
<g stroke-linejoin="round">
<path d="M150 0 L75 200 L225 200 Z" />
<path d="M150 0 L75 200 L225 200 Z" />
<path d="M150 0 L75 200 L225 200 Z" />
</g>
</g>
</g>
</svg>
理想情况下,<g> 元素不应与单个子元素 <g> 保持一致。换句话说:
<g>
<g>
...
</g>
</g>
会变成
<g>
...
</g>
这是我的尝试:
const collapseGroups = node => {
if (node.children && node.children.length > 0) {
node.children.forEach(child => {
collapseGroups(child)
})
if (node.name == 'g' &&
node.children.length == 1 &&
node.children[0].name == 'g') {
node.children = node.children[0].children;
}
}
}
注意:我的输入是一个 SVG 文件,它已经被自动解析成上面提到的 JS 对象。
【问题讨论】:
-
我会为此而绞尽脑汁,但你标题中问题的答案是“致电消防部门”。
-
数据结构是什么?一个字符串?
-
@trincot 在问题中添加了额外信息
-
我认为他 (@Miki) 想要这样的东西:
[ [data] ]->[data] -
好的,很清楚。已发布答案。
标签: javascript recursion data-structures recursive-datastructures