반복문, 즉 for-of 루프

WBOY
풀어 주다: 2024-08-24 11:06:35
원래의
1072명이 탐색했습니다.

Iteration Stament i.e for-of loop

대상:

  • ES6에서 도입됨
  • 일반적으로 루프에는 카운터, 확인 조건, 업데이트 카운터가 있습니다. for-of 루프에는 그런 것이 없습니다.
  • continu-break 둘 다 함께 사용할 수 있습니다.
  • 현재 요소를 제공하기 위한 것입니다.
fruits = ['banana','apple','peach','orange','mango','guava','water-melon'];

for(const item of fruits){
  console.log(item);
}

'banana'
'apple'
'peach'
'orange'
'mango'
'guava'
'water-melon'
로그인 후 복사
- If an array if looped over in the form of array.entries(), then the result will be each element in form of an array with index : value.

for(const item of fruits.entries()){
  console.log(item);
}

[ 0, 'banana' ] 
[ 1, 'apple' ] 
[ 2, 'peach' ] 
[ 3, 'orange' ] 
[ 4, 'mango' ] 
[ 5, 'guava' ] 
[ 6, 'water-melon' ]

// Transform it into a single array comprising of sub-arrays:
fruits.entries(); // Object [Array Iterator] {}

[...fruits.entries()]; 
// [ [ 0, 'banana' ], [ 1, 'apple' ], [ 2, 'peach' ], [ 3, 'orange' ], [ 4, 'mango' ], [ 5, 'guava' ], [ 6, 'water-melon' ] ]

// Transform into a single array using for-of loop:
-> Method 1
for(const item of fruits.entries()){
  console.log(`${item[0] + 1} : ${item[1]}`);
}
// '1 : banana' '2 : apple' '3 : peach' '4 : orange' '5 : mango' '6 : guava' '7 : water-melon'

-> Method 2
for(const [i,el] of fruits.entries()){
  console.log(`${i + 1} : ${el}`);
}
// '1 : banana' '2 : apple' '3 : peach' '4 : orange' '5 : mango' '6 : guava' '7 : water-melon'

로그인 후 복사

위 내용은 반복문, 즉 for-of 루프의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!