当前位置: 首页> 教育> 大学 > 自己怎么设计公司前台设计效果图_工作临时工_开网站需要多少钱_扬州网络推广公司

自己怎么设计公司前台设计效果图_工作临时工_开网站需要多少钱_扬州网络推广公司

时间:2025/7/23 9:14:48来源:https://blog.csdn.net/alicca/article/details/142438610 浏览次数:0次
自己怎么设计公司前台设计效果图_工作临时工_开网站需要多少钱_扬州网络推广公司

目录

1 JSON 字符串化和解析

2 加权树结构的字符串解析对象

3. 解析并返回DOM 树结构

4. 解析带有层级的文本


1 JSON 字符串化和解析

//  <!-- 实现一个函数,能够将自定义格式的字符串转换为 JSON 对象。
//      例如,给定字符串 'name: "Alice", age: 30',输出 { name: "Alice", age: 30 }。 -->
function digui(str){//首先 截取,的前后 然后截取:的前后let obj={}//str1='' x.slice()let arr=str.split(',')//['name:'alice],['age':'30']//考虑到alice外的引号和:后的空白没arr.map((item,index)=>{let [apple,pine]=item.split(': ').map((i)=>i.trim())pine=JSON.parse(pine)obj[apple]=pine})return obj}
let str='name: "Alice", age: 30'
console.log(digui(str))

2 加权树结构的字符串解析对象

给定一个加权树结构的字符串,例如 '(A:3,(B:2,C:1))',解析为一个对象 { A: 3, B: 2, C: 1 }。

//遇到左括号 递归 处理去掉逗号后 去掉冒号 第function digui(str){
//干脆去除所有括号 
let str2=''
for(let i=0;i<str.length;i++){if(str[i]!='(' && str[i]!=')')str2+=str[i]}
let obj={}
let lines=str2.split(',').map(item => item.trim())
//[a:3],[b,3]
lines.forEach((item)=>{let [apple,pine]=item.split(':').map(item => item.trim())obj[apple]=pine})
return obj}let str='(A:3,(B:2,C:1))'console.log(digui(str))

3. 解析并返回DOM 树结构

实现一个函数,能够解析简单的 HTML 字符串并返回 DOM 树结构。

// 例如,给定字符串 "<div><span>Hello</span><p>World</p></div>"

function apple(str){
let parser = new DOMParser()
let tryparser = parser.parseFromString(str,'text/html').body.firstChild
return tryparser}let str= "<div><span>Hello</span><p>World</p></div>";console.log(apple(str)); 

解析后返回的文档中包含一个完整的 HTML 文档结构(包含 <html><head> 和 <body> 标签),.body 选择其中的 <body> 元素。firstChild 选择 <body> 中的第一个子节点div。

text/html' 告诉浏览器和 JavaScript 解释器要将字符串按照 HTML 格式解析。

4. 解析带有层级的文本

编写一个函数将其解析为树形结构。

function treee(str){let root={value:'root',children:[]}let st=[root]let lines=str.split('\n')lines.forEach(line=>{
let level=line.search(/\S/)
let node={value:line.trim(),children:[]}
while(st.length>level+1){st.pop()
}
st[st.length-1].children.push(node)
st.push(node)})return root }const text = 'A\n  B\n    C\n  D';console.log(JSON.stringify(treee(text), null, 2));

关键字:自己怎么设计公司前台设计效果图_工作临时工_开网站需要多少钱_扬州网络推广公司

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: