es6模板字符串中循环遍历数据踩坑

news/2024/7/11 0:55:26 标签: es6, 前端, java

应该很少有这种需求,需要在模板字符串利循环遍历数据,现在框架都有自己模板语法。

我这里踩的坑是,我在模板字符串中用forEach,map遍历循环时,返回值一直是一个undefined
用for循环return之后当条件满足时又直接跳出循环了

有大佬看到这里知道的希望能帮帮忙解答一下

代码如下:
for循环

javascript"> 这是在模板字符串中用for循环遍历
 this.goodsData = [
 {type: '自闭阀', userName: 'zs', userCode: '123'},
 {type: '波纹管', userName: 'zs2', userCode: '142'},
 {type: '报警器', userName: 'zsw', userCode: '172'},
 {type: '表头', userName: 'zsf', userCode: '172'},
 {type: '金属头', userName: 'zsd', userCode: '132'}
 ]
			`
            <div class="install-situation">
              安装情况
            </div>
              ${
                (() => {
                    for (let i = 0; i < this.goodsData.length; i++) {
                      let item = this.goodsData[i];
                      if(item.type == '自闭阀') {
                        return `
                          <div class="install-item">
                            <div class="install-item-frist">${ item.type }</div>
                            <div class="install-item-isright">
                              <div class="install-item-top">是否正常安装</div>
                              <div class="install-item-bottom"></div>
                            </div>
                            <div class="install-item-common">
                              <div class="install-item-top">品牌名称</div>
                              <div class="install-item-bottom">${ item.userName || ''}</div>
                            </div>
                            <div class="install-item-common">
                              <div class="install-item-top">规格型号</div>
                              <div class="install-item-bottom">${ item.model || ''}</div>
                            </div>
                       
                            <div style="display: flex; flex: 6;">
                              <div class="install-item-address">
                                <div class="install-item-top">备注(双螺/一螺一插)</div>
                                <div class="install-item-bottom"></div>
                              </div>
                              <div class="install-item-address-no-border">
                                <div class="install-item-top">是否进行使用培训</div>
                                <div class="install-item-bottom"></div>
                              </div>
                            </div>
                          </div>
                        `
                      }else if (item.type == '波纹管') {
                        return `
                          <div class="install-item">
                            <div class="install-item-frist">${ item.type }</div>
                            <div class="install-item-isright">
                              <div class="install-item-top">是否正常安装</div>
                              <div class="install-item-bottom"></div>
                            </div>
                            <div class="install-item-common">
                              <div class="install-item-top">品牌名称</div>
                              <div class="install-item-bottom">${ item.userName || ''}</div>
                            </div>
                            <div class="install-item-common">
                              <div class="install-item-top">规格型号</div>
                              <div class="install-item-bottom">${ item.model || ''}</div>
                            </div>
                          
                            <div style="display: flex;  flex: 6;">
                              <div class="install-item-address">
                                <div class="install-item-top">备注(双螺/一螺一插)</div>
                                <div class="install-item-bottom"></div>
                              </div>
                              <div class="install-item-address-no-border">
                                <div class="install-item-top">未安装原因</div>
                                <div class="install-item-bottom"></div>
                              </div>
                            </div>
                          </div>
                        `
                      }else {
                        return `
                          <div class="install-item">
                            <div class="install-item-frist">${ item.type }</div>
                            <div class="install-item-isright">
                              <div class="install-item-top">是否正常安装</div>
                              <div class="install-item-bottom"></div>
                            </div>
                            <div class="install-item-common">
                              <div class="install-item-top">品牌名称</div>
                              <div class="install-item-bottom">${ item.userName || ''}</div>
                            </div>
                            <div class="install-item-common">
                              <div class="install-item-top">规格型号</div>
                              <div class="install-item-bottom">${ item.model || ''}</div>
                            </div>
                            <div style="display: flex;  flex: 6;">
                              <div class="install-item-address">
                                <div class="install-item-top">安装位置</div>
                                <div class="install-item-bottom"></div>
                              </div>
                              <div class="install-item-common">
                                <div class="install-item-top">通电情况</div>
                                <div class="install-item-bottom"></div>
                              </div>
                              <div class="install-item-end">
                                <div class="install-item-top">报警测试情况</div>
                                <div class="install-item-bottom"></div>
                              </div>
                            </div>
                        </div>
                        `
                      }
                    }
                })()
              }
              `

我用for循环时条件满足就跳出循环了 导致只能渲染一条数据出来如图:
在这里插入图片描述
期望是这样的:
在这里插入图片描述
可是当我用forEach或者map时 ,直接给我返回一个undefind
不说完整逻辑, 就下面这一点,我在模板字符串中写也是给我返回undefind

this.goodsData.forEach((item) => {
	return '哈哈哈哈或'
})

在此记录一下这个坑
难道模板字符串中不支持forEach,map等之类的遍历吗


http://www.niftyadmin.cn/n/167134.html

相关文章

JavaWeb《三》Request请求转发与Response响应

&#x1f34e;道阻且长&#xff0c;行则将至。&#x1f353; 本文是javaweb的第三篇&#xff0c;介绍了Request请求转发与Response响应。 上一篇&#xff1a;JavaWeb《二》Servlet、Request请求 下一篇&#xff1a;敬请期待 目录一、Request请求转发&#x1f34f;二、Response对…

【业务安全-01】业务安全概述及其测试流程

业务安全概述近年来&#xff0c;随着信息化技术的迅速发展和全球一体化进程的不断加快&#xff0c;计算机和网络已经成为与所有人都息息相关的工具和媒介&#xff0c;个人的工作、生活和娱乐&#xff0c;企业的管理乃至国家的发展和改革都无处其外。信息和互联网带来的不仅仅是…

Python解题 - CSDN周赛第38期

又来拯救公主了。。。本期四道题还是都考过&#xff0c;而且后面两道问哥在以前写的题解里给出了详细的代码&#xff08;当然是python版&#xff09;&#xff0c;直接复制粘贴就可以过了——尽管这样显得有失公允&#xff0c;考虑到以后还会出现重复的考题&#xff0c;所以现在…

Windos远程连接Linux方法

ssh安装 使用root用户登录 su root 更换apt 下载源为清华源&#xff0c;先备份原来的源 sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup vim /etc/apt/sources.list 替换为下面连接里的代码 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 更新apt配置su…

微软研究院玻璃存储项目Project Silica最新进展

微软研究院关于数据冷热分层以及对应的存储技术描述如下图所示&#xff1a; 实际上&#xff0c;在归档存储&#xff08;Archive Storage&#xff09;这一层中还可以分出需要长期保存的重要数据&#xff0c;即档案类数据、资产类数据、法规遵从类数据&#xff0c;对于这一类重要…

基于java下Springboot框架实现社区维修平台系统

基于java下Springboot框架实现社区维修平台系统开发语言&#xff1a;Java 框架&#xff1a;springboot JDK版本&#xff1a;JDK1.8 服务器&#xff1a;tomcat7 数据库&#xff1a;mysql 5.7 数据库工具&#xff1a;Navicat11 开发软件&#xff1a;eclipse/myeclipse/idea Maven…

重构数据-Encapsulate Collection封装集合十

重构数据-Encapsulate Collection封装集合十 1.封装集合 1.1.使用场景 有个函数返回一个集合。让这个函数返回该集合的一个只读副本&#xff0c;并在这个类中提供添加/移除集合元素的函数。 我们常常会在一个类中使用集合&#xff08;collection&#xff0c;可能是array、l…

Java 泛型

Java泛型是指一种抽象的类型&#xff0c;它可以用来表示多种类型的对象。Java中的泛型是通过参数化类型来实现的&#xff0c;这使得我们可以编写通用的代码&#xff0c;而不用考虑数据类型的具体实现。 Java泛型的出现是为了解决Java语言在类型安全方面的问题。在Java泛型出现…