JavaScript遍历JSON对象:嵌套for循环处理技术与项目数据问题
代码核心问题排查
- 对象键重复导致数据丢失:每个技术分类下重复定义了多个同名
project键,JS对象键名唯一的规则会触发值覆盖,最后每个分类下只会保留最后一个项目,前面两个项目会被完全覆盖,根本遍历不到。 - 嵌套对象同样存在键重复问题:项目内的
tech对象重复定义了techname键,同样会触发值覆盖,最后只能读到最后一个技术栈值,无法保留全部关联技术。 - 循环作用域逻辑错误:用
var声明的循环变量是函数/全局作用域,内层循环每执行一次就会重新赋值holderkey_n,外层循环拼接DOM内容时,拿到的永远是内层循环最后一次的固定值,无法对应每类技术的真实项目数据。 - 数据结构不匹配遍历需求:一对多的关联关系(一类技术对应多个项目、一个项目对应多个技术栈)用嵌套对象存储本身不符合语法规则,这类多值场景应该用数组结构存储内容列表。
修正后的可运行实现代码
// 修正结构:重复键替换为数组存储多值内容 var projects = { ReactJs: [ { name: "Lorem Ipsum", industry: "Health", technologies: "XD, HTML5, CSS, ReactNative, Redux", description: "Tripsafe is an app designed to address cross-border travel issues both nationally and internationally. With automated uploads from Pathologists, Doctors or any health professional relating to tests, certificates, letters and or official visas, everyone can check, verify and confirm that the document seen is a true immutable digital copy.", img: "project/img", tech: ["ReactJs", "ReactNative"] }, { name: "Lorem Ipsum", industry: "Event management", description: "In this app you can apply to be a member of the United Leuva of Houston if accepted you can then receive information about the organization via this application.To get the latest news about United Leuva Houston you it would be via this app.", img: "project/img", tech: ["XD", "HTML5", "CSS", "ReactNative", "Redux"] }, { name: "Lorem Ipsum", industry: "Hospitality", description: "This app is intended to be used in the hospitality industry. The app RoomCheck is an in-house product of SJRP Systems. The app streamlines processes for housekeeping and maintenance by making their work and work progress real time.", img: "project/img", tech: ["ReactNative", "Redux"] } ], ReactNative: [ { name: "Lorem Ipsum", industry: "Health", technologies: "XD, HTML5, CSS, ReactNative, Redux", description: "Tripsafe is an app designed to address cross-border travel issues both nationally and internationally. With automated uploads from Pathologists, Doctors or any health professional relating to tests, certificates, letters and or official visas, everyone can check, verify and confirm that the document seen is a true immutable digital copy.", img: "project/img", tech: ["ReactJs", "ReactNative"] }, { name: "Lorem Ipsum", industry: "Event management", description: "In this app you can apply to be a member of the United Leuva of Houston if accepted you can then receive information about the organization via this application.To get the latest news about United Leuva Houston you it would be via this app.", img: "project/img", tech: ["XD", "HTML5", "CSS", "ReactNative", "Redux"] }, { name: "Lorem Ipsum", industry: "Hospitality", description: "This app is intended to be used in the hospitality industry. The app RoomCheck is an in-house product of SJRP Systems. The app streamlines processes for housekeeping and maintenance by making their work and work progress real time.", img: "project/img", tech: ["ReactNative", "Redux"] } ] } var data = ''; // 外层循环遍历所有开发技术分类,生成独立展示区块 for (let techName in projects) { data += `<section class="tech-block"><h2>${techName} 技术栈项目</h2><div class="project-list">`; // 内层循环遍历当前分类下的所有项目,嵌入对应区块 projects[techName].forEach(project => { data += ` <div class="project-card"> <h3>${project.name}</h3> <p>所属行业:${project.industry}</p> <p>关联技术:${project.tech.join('、')}</p> <p>项目描述:${project.description}</p> <img src="${project.img}" alt="${project.name}"> </div> ` }) data += `</div></section>` } document.getElementById('main').innerHTML = data;
修正逻辑说明
- 将所有重复定义的对象键替换为数组结构,从根源上解决值覆盖导致的数据丢失问题
- 用块级作用域的
let替代var声明循环变量,避免作用域污染带来的取值错误 - 遍历流程完全匹配展示需求:每类技术先生成独立的区块容器,再将该分类关联的所有项目内容嵌入容器内部
内容的提问来源于stack exchange,提问作者Lokesh Thakur
相关产品推荐
相关产品推荐

