当前位置:网站首页>Array_ 01return in foreach
Array_ 01return in foreach
2022-07-24 05:30:00 【Good [email protected]】
return keyword
break And continue keyword
break And continue Keyword Introduction
forEach Medium return
[1]forEach The function is to cycle the array , But the essence is a function , therefore stay forEach It can't be used directly break And continue To stop the cycle ( Will report a mistake );
[2] that forEach How to terminate the loop ?
- forEach The essence is a function , Use return You can terminate the execution of the function ;
- Encapsulate inside the function
- return true amount to continue; End this cycle , Execute next cycle ;
- return false amount to break ; End of cycle , Execute the statement after the loop ;
[3] If used inside a function forEach
function getNum(arr){ let sum = 0 arr.forEach(item=>{ sum +=item if(item == 5){ return sum } }) return sum } const sum = getNum([1,3,5,7,9]) console.log(' result ',sum) // 25- stay item == 5 when ,sum by 9 Convert to boolean When the value is 0 true; —> stay forEach Inside return true It will be resolved to continue( Jump out of this cycle and execute the next cycle );
- So you can see that if you want to use loops inside functions and inside loops return Words , Out of commission forEach For loop traversal !
- stay item == 5 when ,sum by 9 Convert to boolean When the value is 0 true; —> stay forEach Inside return true It will be resolved to continue( Jump out of this cycle and execute the next cycle );
版权声明
本文为[Good [email protected]@@]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207240515392877.html
边栏推荐
猜你喜欢
随机推荐
JS链表中的快慢指针
你真的知道判断语句吗?
LaTeX学习笔记(一)——安装配置
Promise
关键字_01return
浏览器的本地存储
Text summary acl2021
Handwritten ORM framework
Constructor_ Map constructor
C语言进阶篇 三.字符串函数和内存操作函数
新语法01_Es6新语法
A collation of the basic usage of numpy
JDBC encapsulates a parent class to reduce code duplication
根据数组中对象的某个属性值进行排序
special effects - 鼠标点击,出现烟花炸裂效果
输入若干数据,找出最大值输出。(键盘和文件读取)
Relationship between sample and population in Statistics: sample success ratio + central limit theorem (sample mean)
C语言入门篇 三.数组 四.操作符
Detailed explanation of string constant pool and intern() of string
文本摘要 ACL2021









