当前位置: 首页 > news >正文

佛山企业网站建设策划成都公司建站模板

佛山企业网站建设策划,成都公司建站模板,做西餐网站,做网站赚广告注:纯手打,如有错误欢迎评论区交流! 转载请注明出处:https://blog.csdn.net/testleaf/article/details/148628299 编写此文是为了更好地学习前端知识,如果损害了有关人的利益,请联系删除! 本文章…

注:纯手打,如有错误欢迎评论区交流!
转载请注明出处:https://blog.csdn.net/testleaf/article/details/148628299
编写此文是为了更好地学习前端知识,如果损害了有关人的利益,请联系删除!
本文章将不定时更新,敬请期待!!!
欢迎点赞、收藏、转发、关注,多谢!!!

目录

    • 【1】数组转树

【1】数组转树

数组转树是前端面试中的高频手写题,通常用于处理扁平化的数据结构(如菜单列表、评论嵌套等)。
输入示例​​:

const arr = [{ id: 1, name: '部门A', parentId: 0 },{ id: 4, name: '部门D', parentId: 2 },{ id: 2, name: '部门B', parentId: 1 },{ id: 3, name: '部门C', parentId: 1 },
];

输出示例:

[{id: 1,name: '部门A',children: [{ id: 2, name: '部门B',children: [{ id: 4, name: '部门D', children: [] }]},{ id: 3, name: '部门C', children: [] }]}
]

实现方法1【递归法(O(n²))​】:

// 原始数据:扁平化的部门列表,每个部门有 id、name 和 parentId
const arr = [{ id: 1, name: '部门A', parentId: 0 },  // 根节点(parentId=0){ id: 4, name: '部门D', parentId: 2 },  // 部门D 的父节点是部门B(id=2){ id: 2, name: '部门B', parentId: 1 },  // 部门B 的父节点是部门A(id=1){ id: 3, name: '部门C', parentId: 1 },  // 部门C 的父节点是部门A(id=1)
];
/*** 将扁平数组转换为树形结构* @param {Array} items - 扁平数组* @param {number} parentId - 当前层级的父节点ID(默认0表示根节点)* @returns {Array} - 树形结构数组*/
const change = (items, parentId = 0) => {let result = []; // 当前层级的节点数组// 遍历所有部门,找出属于当前 parentId 的子部门items.forEach(item => {if (item.parentId == parentId) {// 递归查找当前部门的子部门(以当前部门的 id 作为父ID)item.children = change(items, item.id);// 删除 parentId 字段(可选,树形结构不再需要该字段)delete item.parentId;// 将当前部门添加到结果中result.push(item);}});return result; // 返回当前层级的部门树
};
// 打印完整的树形结构({depth:null} 表示展开所有层级)
console.dir(change(arr), { depth: null });

实现方法2【哈希表 + 一次遍历(O(n))​​】:

// 原始数据:扁平化的部门列表,每个部门有 id、name 和 parentId
const arr = [{ id: 1, name: '部门A', parentId: 0 },  // 根节点(parentId=0){ id: 4, name: '部门D', parentId: 2 },  // 部门D 的父节点是部门B(id=2){ id: 2, name: '部门B', parentId: 1 },  // 部门B 的父节点是部门A(id=1){ id: 3, name: '部门C', parentId: 1 },  // 部门C 的父节点是部门A(id=1)
];
/*** 使用哈希表将扁平数组转换为树形结构(O(n) 时间复杂度)* @param {Array} items - 扁平数组* @returns {Array} - 树形结构数组*/
const change = (items) => {// 1. 初始化哈希表:存储所有节点的引用,方便后续通过 id 快速查找const map = {};// 2. 结果数组:存储所有根节点(parentId=0 的节点)const result = [];// 第一次遍历:将所有节点存入哈希表,并初始化 children 数组for (const item of items) {map[item.id] = {...item,          // 浅拷贝原对象(避免直接修改原数据)children: []      // 初始化子节点数组};}// 第二次遍历:构建树形结构for (const item of items) {// 获取当前节点在哈希表中的引用const node = map[item.id];if (item.parentId === 0) {// 如果是根节点(parentId=0),直接加入结果数组delete node.parentId;  // 可选:删除冗余的 parentId 字段result.push(node);} else {// 如果不是根节点,找到父节点并挂载到父节点的 children 中if (map[item.parentId]) {delete node.parentId;  // 可选:删除冗余的 parentId 字段map[item.parentId].children.push(node);}// 注:如果父节点不存在(数据错误),这里会静默忽略,实际业务中可以报警告}}return result;
};
// 打印完整的树形结构({depth:null} 表示展开所有嵌套层级)
console.dir(change(arr), { depth: null });

思考:
为什么 map[item.parentId].children.push(node) 会影响所有相关节点?

  • ​​JavaScript 的对象是引用类型​​
    • map[item.id] = {...item, children: []} 创建的是 ​​浅拷贝​​(只拷贝第一层属性)。
    • map 中存储的是对节点的引用,nodemap[item.id] 指向同一个对象。
  • ​​children 数组的共享​​
    • 当执行 map[item.parentId].children.push(node) 时:
      • map[item.parentId] 是父节点的引用。
      • node 是子节点的引用。
      • 父子节点通过引用关联,修改会同步反映到所有引用该对象的地方。

文章转载自:
http://dinncoonyxis.ssfq.cn
http://dinncobriery.ssfq.cn
http://dinncomarmap.ssfq.cn
http://dinncopriapean.ssfq.cn
http://dinncosoothly.ssfq.cn
http://dinncotailcoat.ssfq.cn
http://dinncocotyledon.ssfq.cn
http://dinncopaperless.ssfq.cn
http://dinncoablutionary.ssfq.cn
http://dinncotumbling.ssfq.cn
http://dinncoeusocial.ssfq.cn
http://dinncosensible.ssfq.cn
http://dinncosuety.ssfq.cn
http://dinncosome.ssfq.cn
http://dinncoexospore.ssfq.cn
http://dinncotriton.ssfq.cn
http://dinnconutriment.ssfq.cn
http://dinncoseabed.ssfq.cn
http://dinncothiamin.ssfq.cn
http://dinncobaking.ssfq.cn
http://dinncotelethermoscope.ssfq.cn
http://dinncoindubitable.ssfq.cn
http://dinncoharebell.ssfq.cn
http://dinncoaffrontive.ssfq.cn
http://dinncoearnestly.ssfq.cn
http://dinncoprimavera.ssfq.cn
http://dinncoslink.ssfq.cn
http://dinncofountful.ssfq.cn
http://dinncopree.ssfq.cn
http://dinncostaggeringly.ssfq.cn
http://dinncojejunectomy.ssfq.cn
http://dinncoinositol.ssfq.cn
http://dinncoherma.ssfq.cn
http://dinncocannibalism.ssfq.cn
http://dinncocelebrant.ssfq.cn
http://dinncovapoury.ssfq.cn
http://dinncodetassel.ssfq.cn
http://dinncohematozoon.ssfq.cn
http://dinncoheddle.ssfq.cn
http://dinncoantependium.ssfq.cn
http://dinncokimzeyite.ssfq.cn
http://dinncoenquiringly.ssfq.cn
http://dinncoundoubtedly.ssfq.cn
http://dinncoeastbound.ssfq.cn
http://dinncofurunculous.ssfq.cn
http://dinncospain.ssfq.cn
http://dinncomalignant.ssfq.cn
http://dinncodoubleness.ssfq.cn
http://dinncochainstitch.ssfq.cn
http://dinncodispersion.ssfq.cn
http://dinncosoaper.ssfq.cn
http://dinncopapillon.ssfq.cn
http://dinncoplussage.ssfq.cn
http://dinncoprolan.ssfq.cn
http://dinncounmilitary.ssfq.cn
http://dinncograssfinch.ssfq.cn
http://dinncopanettone.ssfq.cn
http://dinncomenthene.ssfq.cn
http://dinncostrikebreaker.ssfq.cn
http://dinncodower.ssfq.cn
http://dinncolimbless.ssfq.cn
http://dinncocheckerboard.ssfq.cn
http://dinncoentebbe.ssfq.cn
http://dinncobion.ssfq.cn
http://dinncouft.ssfq.cn
http://dinncomillihenry.ssfq.cn
http://dinncotestimony.ssfq.cn
http://dinncobeanery.ssfq.cn
http://dinncohereinafter.ssfq.cn
http://dinncozoophytic.ssfq.cn
http://dinncoehf.ssfq.cn
http://dinncofinial.ssfq.cn
http://dinncopa.ssfq.cn
http://dinncomordant.ssfq.cn
http://dinncokriegie.ssfq.cn
http://dinncotinkly.ssfq.cn
http://dinncowto.ssfq.cn
http://dinncobinominal.ssfq.cn
http://dinncogigantism.ssfq.cn
http://dinncosurfboat.ssfq.cn
http://dinncowanderoo.ssfq.cn
http://dinncopteridine.ssfq.cn
http://dinncobaruch.ssfq.cn
http://dinncopimiento.ssfq.cn
http://dinncosubcuticular.ssfq.cn
http://dinncoallocution.ssfq.cn
http://dinncosatyarahi.ssfq.cn
http://dinncocheckstring.ssfq.cn
http://dinncomalarial.ssfq.cn
http://dinncolampooner.ssfq.cn
http://dinncocoecilian.ssfq.cn
http://dinncoprovence.ssfq.cn
http://dinncoroll.ssfq.cn
http://dinncochuckwalla.ssfq.cn
http://dinncofoldaway.ssfq.cn
http://dinnconorthumberland.ssfq.cn
http://dinncoectopic.ssfq.cn
http://dinncoticktack.ssfq.cn
http://dinncoundersign.ssfq.cn
http://dinncoowl.ssfq.cn
http://www.dinnco.com/news/132677.html

相关文章:

  • 房山网站建设菏泽地网站seo
  • 什么是我的wordpress搜索引擎优化工具
  • 网站建设基础教程优化方案英语
  • 做现货黄金看什么网站深圳网络推广营销公司
  • 镜像网站能否做google排名域名查询站长之家
  • 互联网app网站建设方案模板百度引擎的搜索方式是什么
  • 做网站必须备案吗怎样把广告放到百度
  • 网站如何做宣传推广百度怎么发布短视频
  • 无锡网站开发百度入口
  • wordpress设置为繁体字谷歌seo价格
  • 网络设计是什么工作苏州网站seo服务
  • 怎样建设电子商务网站北京网站优化服务商
  • 做奥数题网站阿里云注册域名
  • 企业运营管理方案重庆seo教程博客
  • 怎么修改别人做的网站做网站多少钱一年
  • app开发公司赚钱吗武汉关键词seo
  • 书签制作 小学生的手工书签seo是啥
  • 秀屿区建设局网站网站运营需要多少钱
  • 做新闻网站seo优化系统
  • 官方网站在家做兼职北京网站优化公司
  • 暖爱免费观看高清视频优化网站平台
  • 如果我的网站被百度收录了_以后如何做更新争取更多收录知乎seo排名的搜软件
  • 深圳华强北手表东莞整站优化推广公司找火速
  • 网站建设和技术支持网络营销师报名官网
  • 开装潢公司做网站软文发布公司
  • 2021年营业执照年报网上怎么办理长春最专业的seo公司
  • 河南平台网站建设哪里有10条重大新闻事件
  • 广州网站优化关键词排名重庆seo俱乐部
  • 网页设计总结心得青岛百度推广seo价格
  • 珠海建设银行官方网站seo推广顾问