应该很少有这种需求,需要在模板字符串利循环遍历数据,现在框架都有自己模板语法。
我这里踩的坑是,我在模板字符串中用forEach,map遍历循环时,返回值一直是一个undefined
用for循环return之后当条件满足时又直接跳出循环了
有大佬看到这里知道的希望能帮帮忙解答一下
代码如下:
for循环
这是在模板字符串中用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'}]`安装情况${(() => {for (let i = 0; i < this.goodsData.length; i++) {let item = this.goodsData[i];if(item.type == '自闭阀') {return `${ item.type }是否正常安装品牌名称${ item.userName || ''}规格型号${ item.model || ''}备注(双螺/一螺一插)是否进行使用培训`}else if (item.type == '波纹管') {return `${ item.type }是否正常安装品牌名称${ item.userName || ''}规格型号${ item.model || ''}备注(双螺/一螺一插)未安装原因`}else {return `${ item.type }是否正常安装品牌名称${ item.userName || ''}规格型号${ item.model || ''}安装位置通电情况报警测试情况`}}})()}`
我用for循环时条件满足就跳出循环了 导致只能渲染一条数据出来如图:
期望是这样的:
可是当我用forEach或者map时 ,直接给我返回一个undefind
不说完整逻辑, 就下面这一点,我在模板字符串中写也是给我返回undefind
this.goodsData.forEach((item) => {return '哈哈哈哈或'
})
在此记录一下这个坑
难道模板字符串中不支持forEach,map等之类的遍历吗