gpt4 book ai didi

javascript - 如何使这个 javascript 动态 dom 创建函数无状态?

转载 作者:行者123 更新时间:2023-11-30 20:17:15 26 4
gpt4 key购买 nike

我一直在研究这个问题,并找到了解决方案。

const TREE_DATA = {
"widgetData":[
{
"label":"node1",
"color":"red",
"children":[
{
"label":"vip1",
"color":"red",
"children":[
{
"label":"obj1",
"color":"gray",
"id":"539803eae4b0ffad82491508"
},
{
"label":"obj2",
"color":"green",
"id":"5395635ee4b071f136e4b691"
},
{
"label":"obj3",
"color":"green",
"id":"539803e4e4b0ffad82491507"
}
],
"id":"53956358e4b071f136e4b690"
},
{
"label":"vip2",
"color":"blue",
"id":"539803f2e4b0ffad82491509"
}
],
"id":"5395634ee4b071f136e4b68e"
},
{
"label":"node2",
"children":[
{
"label":"vip1",
"color":"green",
"id":"539803eae4b0ffad82491501"
},
{
"label":"vip2",
"color":"green",
"id":"5395635ee4b071f136e4b694"
}
],
"id":"5395637fe4b071f136e4b692"
},
{
"label":"node3",
"color":"red",
"children":[

],
"id":"53956371f136e4b692"
},
{
"label":"node4",
"color":"red",
"children":[

],
"id":"5656"
},
{
"label":"node5",
"color":"red",
"children":[
],
"id":"5395637fe4b071f13b692"
}
]
}

const createDOM = (arr) => {

function updateDOM(arr, html, el) {
if (typeof (html) === 'undefined') {
var html = '';
}
if (typeof (el) === 'undefined') {
var el = {li: '<li>', liEnd: '</li>', ul: '<ul>', ulEnd: '</ul>'};
}
if (typeof (arr) === 'string') {
return el.li + arr + el.liEnd;
} else if (typeof (arr) === 'object') {
for (var item in arr) {

if (typeof (arr[item]) === 'string') {
html += el.ul
+ el.li + arr[item] + el.liEnd
+ el.ulEnd;
} else if(typeof (item) === 'string' && (isNaN(item))){
html += el.ul
+ updateDOM(arr[item],'',el)
+ el.ulEnd;

} else if (typeof (arr[item]) === 'object') {
html = updateDOM(arr[item], html,el);
}
}
}
return html;
}
document.getElementById('tree').innerHTML = updateDOM(arr);

}


createDOM(TREE_DATA);
<div id="tree"></div>

但是,我想了解如何使其成为无状态的?比如,如果我只想将 TREE_DATA 传递到内部 updateDOM 函数并返回 dom 树,我该怎么做?我希望能够以不必在每次递归函数调用中传递 HTML 的方式编写此函数。如何处理中间 HTML?

例如下面的斐波那契函数的组成:

function factorial(n) {
if (n === 0) {
return 1;
} else {
return n * factorial(n-1);
}
}

最佳答案

我假设你不想要额外的 <ul> s 在输出中。如果这样做,就很容易改变。

您不需要定义 el在函数中。由于它是常量并且更像是实用程序查找,因此您可以在函数外定义它。您也可以只内联 HMTML 标签。

要仅通过传入树来完成这项工作,您需要在每次调用时返回一些内容并将其附加到先前调用的结果中。您可以在 for 循环中追加到单个 html 字符串,并递归地传递找到的元素。我通过使用 Array.isArray() 测试数组来简化内部调用。 , 但如果你愿意,你应该能够保留自己的逻辑:

const TREE_DATA = {"widgetData":[{"label":"node1","color":"red","children":[{"label":"vip1","color":"red","children":[{"label":"obj1","color":"gray","id":"539803eae4b0ffad82491508"},{"label":"obj2","color":"green","id":"5395635ee4b071f136e4b691"},{"label":"obj3","color":"green","id":"539803e4e4b0ffad82491507"}],"id":"53956358e4b071f136e4b690"},{"label":"vip2","color":"blue","id":"539803f2e4b0ffad82491509"}],"id":"5395634ee4b071f136e4b68e"},{"label":"node2","children":[{"label":"vip1","color":"green","id":"539803eae4b0ffad82491501"},{"label":"vip2","color":"green","id":"5395635ee4b071f136e4b694"}],"id":"5395637fe4b071f136e4b692"},{"label":"node3","color":"red","children":[],"id":"53956371f136e4b692"},{"label":"node4","color":"red","children":[],"id":"5656"},{"label":"node5","color":"red","children":[],"id":"5395637fe4b071f13b692"}]}

var el = { li: '<li>', liEnd: '</li>', ul: '<ul>', ulEnd: '</ul>'};

const createDOM = (arr) => {
if (typeof(arr) === 'string') {
return el.li + arr + el.liEnd;
} else if (typeof(arr) === 'object') {
let html = ''
for (var item in arr) {
if (Array.isArray(arr[item])) {
html += el.ul + createDOM(arr[item]) + el.ulEnd
} else {
html += createDOM(arr[item]);
}
}
return html
}

}

document.getElementById('tree').innerHTML = createDOM(TREE_DATA)
<div id="tree"></div>

如果您愿意使用 reduce()Object.values您将其简化为可能更具可读性的内容:

const TREE_DATA = {"widgetData":[{"label":"node1","color":"red","children":[{"label":"vip1","color":"red","children":[{"label":"obj1","color":"gray","id":"539803eae4b0ffad82491508"},{"label":"obj2","color":"green","id":"5395635ee4b071f136e4b691"},{"label":"obj3","color":"green","id":"539803e4e4b0ffad82491507"}],"id":"53956358e4b071f136e4b690"},{"label":"vip2","color":"blue","id":"539803f2e4b0ffad82491509"}],"id":"5395634ee4b071f136e4b68e"},{"label":"node2","children":[{"label":"vip1","color":"green","id":"539803eae4b0ffad82491501"},{"label":"vip2","color":"green","id":"5395635ee4b071f136e4b694"}],"id":"5395637fe4b071f136e4b692"},{"label":"node3","color":"red","children":[],"id":"53956371f136e4b692"},{"label":"node4","color":"red","children":[],"id":"5656"},{"label":"node5","color":"red","children":[],"id":"5395637fe4b071f13b692"}]}


const createDOM = (arr) => {
if (Array.isArray(arr)) {
return arr.reduce((html_string, item) => html_string + "<ul>" + createDOM(item) + "</ul>", '')
}
else if (typeof (arr) === 'object') {
return Object.values(arr).reduce((html_string, val) => html_string + createDOM(val), '')
}
else return "<li>" + arr + "</li>";
}


document.getElementById('tree').innerHTML = createDOM(TREE_DATA)
<div id="tree"></div>

编辑

要使用 DOM 而不是字符串,你基本上要相同的东西,但要用到 dom 元素而不是字符串。

const TREE_DATA = {"widgetData":[{"label":"node1","color":"red","children":[{"label":"vip1","color":"red","children":[{"label":"obj1","color":"gray","id":"539803eae4b0ffad82491508"},{"label":"obj2","color":"green","id":"5395635ee4b071f136e4b691"},{"label":"obj3","color":"green","id":"539803e4e4b0ffad82491507"}],"id":"53956358e4b071f136e4b690"},{"label":"vip2","color":"blue","id":"539803f2e4b0ffad82491509"}],"id":"5395634ee4b071f136e4b68e"},{"label":"node2","children":[{"label":"vip1","color":"green","id":"539803eae4b0ffad82491501"},{"label":"vip2","color":"green","id":"5395635ee4b071f136e4b694"}],"id":"5395637fe4b071f136e4b692"},{"label":"node3","color":"red","children":[],"id":"53956371f136e4b692"},{"label":"node4","color":"red","children":[],"id":"5656"},{"label":"node5","color":"red","children":[],"id":"5395637fe4b071f13b692"}]}


const createDOM = (arr) => {
if (Array.isArray(arr)) {
return arr.reduce((ul, item) => {
ul.appendChild(createDOM(item))
return ul
}, document.createElement('ul'))
}
else if (typeof (arr) === 'object') {
return Object.values(arr).reduce((dom, val) => {
dom.appendChild(createDOM(val))
return dom
}, document.createDocumentFragment())
}
else {
let li = document.createElement('li')
li.appendChild(document.createTextNode(arr))
return li
}
}


document.getElementById('tree').appendChild(createDOM(TREE_DATA))
 <div id="tree"></div>

关于javascript - 如何使这个 javascript 动态 dom 创建函数无状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51810900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com