美文网首页
2021-09-07

2021-09-07

作者: Splendid飞羽 | 来源:发表于2021-09-07 00:33 被阅读0次

````

let arr = [1, 2, 3, 4, 5]

let text = ''

for (let v of arr) {

  if (v === 3) {

    break

  }

  text += v + ','

}

console.log(text) // "1,2,"

````. 

使用some进行代码优化

````

let arr = [1, 2, 3, 4, 5]

let text = ''

arr.some(v => {

  if (v === 3) {

    return true

  }

  text += v + ','

})

console.log(text) // "1,2,"

````

相关文章

网友评论

      本文标题:2021-09-07

      本文链接:https://www.haomeiwen.com/subject/nohuwltx.html