js 数组遍历map踩坑
在react或者vue进行页面渲染时候,我们比较喜欢使用map循环遍历属性相似的节点,例如列表渲染 1 let res: any[] | JSX.Element[] = [] 2 Object.keys(item).forEach((rowItem: any) => { 3 if (rowItem === 'id' || rowItem === 'show' || rowItem === 'text') { 4 return 5 } else { 6 res.push( 7 <div style={{ paddingBottom: '10px' }}> 8 <span className="desc-title">{`${item[rowItem].label}:`}</span> 9 <span className="desc-text">{`${item[rowItem].value}`}</span> 10 </div>, 11 ) 12 } 13 }) 我们在map循环一个数组的时候,在map中加入判断条件例如item.key = id时候,map不会中断条件而继续执行item.key != id的条件,循环中不会直接跳出循环; map和foreach都不会跳出循环 1 let arr = [ 2 {id:'1',num:'1'}, 3 {id:'2',num:'2'},